使用 React 和 Javacript,在通过 props.history.push
传递时向 props.location
添加自定义属性是否可以接受?例如
this.props.history.push({
pathname: '/someurl',
state: {smallObject: myObject},
customProperty : myBiggerObject
});
然后在加载到/someurl 的组件上,我可以加载:
让 myBigObj = this.props.location.customProperty;
这似乎可行,我想问一下这样做“可以”吗?我担心我可能会忽略 Javascript 或 React 中我不知道的某些内容。
如果我传递的对象足够小,我会在 state 属性中传递 customProperty。它不是:它超过了提到的 640k 限制 here ,如果我在 state
属性中传递 myBiggerObject
,我会收到一条错误消息,指出无法克隆数据。
最佳答案
这是 API documentation 的链接
history.push(path, [state])
// Push a new entry onto the history stack.
history.push('/home');
// Push a new entry onto the history stack with a query string
// and some state. Location state does not appear in the URL.
history.push('/home?the=query', { some: 'state' });
// If you prefer, use a single location-like object to specify both
// the URL and state. This is equivalent to the example above.
history.push({
pathname: '/home',
search: '?the=query',
state: { some: 'state' }
});
// Go back to the previous history entry. The following
// two lines are synonymous.
history.go(-1);
history.goBack();
注意:位置状态仅在 createBrowserHistory
和 createMemoryHistory
中受支持。
关于javascript - 将自定义属性添加到 props.location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318868/