2

使用 React 和 Javacript,props.location在通过时添加自定义属性是否可以接受props.history.push?例如

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

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

这似乎可行,我在问这样做是否“可以”?我担心我可能会忽略 Javascript 或 React 中我不知道的某些内容。

如果我传递的对象足够小,我会在 state 属性中传递 customProperty。不是:它超过了此处myBiggerObject提到的 640k 限制,并且我收到一条错误消息,表明如果我传入该state属性,则无法克隆数据。

4

1 回答 1

2

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

注意:位置状态仅在createBrowserHistory和中受支持createMemoryHistory

于 2019-05-27T02:30:54.910 回答