React 使用 state传值, 页面刷新后state为空了

请问下,从A页面跳转到B页面,用state传值,在B页面刷新后 state就为空了,怎么解决?

A页面 传值代码

    render: (text, record, index) => {
      const path = {
        pathname:'/goods-details',
        state: {
          outerItemId: record.outerItemId,
        },
      }
      return <Link to={path}>{text}</Link>;
    },

阅读 13.5k
3 个回答

to: object
An object that can have any of the following properties:
pathname: A string representing the path to link to.
search: A string represenation of query parameters.
hash: A hash to put in the URL, e.g. #a-hash.
state: State to persist to the location.
https://reacttraining.com/rea...

这肯定没了啊,刷新了连redux都没了,更别说state了
解决方法1:把数据放localStorage里,在这个组件加载前,先从rouer的state里取,没有就从localStorage取,再没有就真没有了。能解决,但是不现实。
解决方法2:把这个id放在url的search部分,这样页面刷新还在,然后在这个组件加载前,先从rouer的state里取,没有就用这个id从server端取。

新手上路,请多包涵

改成browserhistory,刷新页面,参数不会刷新

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题