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

时间:2019-05-27 02:06:31

标签: javascript reactjs browser custom-properties

使用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。并非如此:它超出了here所述的640k的限制,并且我收到一条错误消息,指示如果我在myBiggerObject属性中传递state,则无法克隆数据。

1 个答案:

答案 0 :(得分:1)

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

注意:只有createBrowserHistorycreateMemoryHistory支持位置状态。