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

标签 javascript reactjs browser custom-properties

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

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

关于javascript - 将自定义属性添加到 props.location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318868/

相关文章:

javascript - 从 set In 返回一个映射 - Immutable JS

reactjs - 如何获取用户输入(YouTube视频链接)并使用React.js将其嵌入为iframe?

java - Java applet使用智能卡实现数字签名

CSS 不同的浏览器意味着不同的输出

javascript - 在客户端禁用 HTML 元素 (javascript)

javascript - 从 jQuery 中的对象数组动态生成选择选项

javascript - 单击时切换灯箱中的可见区域。 Angular 和 JQuery

javascript - JavaScript 中的奇怪语法

javascript - 功能组件和对象迭代

c# - 适用于 Java 或 C# 的浏览器组件