首页 > 解决方案 > 在反应中将一些值从一页传递到另一页

问题描述

我想从当前页面调用其他页面,现在写我正在使用 props.history.push(destination_page) 但我想将一些值也传递给该目标页面,以便在那里我可以设置默认过滤器。状态管理方法是传递值的唯一方法吗?

标签: reactjs

解决方案


路由状态

props.history.push({
  pathname: destination_page,
  state: {
    payload_to_send,
  },
});

通过接收路线上的位置道具访问

props.location.state.payload_to_send

您还可以在 URL 查询参数中以纯文本形式发送数据,通过 检索props.location.search,但字符串表示整个查询,因此您需要一些额外的逻辑/处理来提取键值对。


推荐阅读