将自定义属性添加到props.location



使用react和javacript,在通过 props.history.push传递时,将自定义属性添加到 props.location是否可以接受?例如

this.props.history.push({
  pathname: '/someurl',
  state: {smallObject: myObject},
  customProperty : myBiggerObject
});

,然后在加载/someurl的组件上,我可以加载: let myBigObj = this.props.location.customProperty;

这似乎有效,我问这是否可以"可以"吗?我担心我可能会忽略JavaScript中的某些内容或我不知道的反应。

如果我通过的对象足够小,我将通过状态属性中的customProperty。它不是:它超过此处提到的640k限制,我会发现一个错误,表明如果我在state属性中通过myBiggerObject,则无法克隆数据。

这是API文档的链接

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();

注意:位置状态仅在createBrowserHistorycreateMemoryHistory中支持。

相关内容

  • 没有找到相关文章

最新更新