【React】路由组件跳转页面并存储参数

记录目前用的比较舒服的方式:

1、跳转

<Button type="secondary" size='small'  >
              <Link to={{ pathname: '/base/strategyList',search:id+'', query: {sceneId:id,record:record} }}>查看</Link>
            </Button>&nbsp;

2、获取值:

  componentDidMount() {
    const {dispatch,location}=this.props;

    let recvParam;

    if(location.query&&location.query.record){//判断当前有参数
      recvParam=location.query.record;
      sessionStorage.setItem('data',recvParam);// 存入到sessionStorage中
    }else{
      recvParam=sessionStorage.getItem('data');// 当state没有参数时,取sessionStorage中的参数
    }

    this.setState({
      recvParam
    })

    console.log("recvParam",recvParam)
  }

3、事件触发:

this.props.history.push(
      {
        pathname: '/dashboard',
         state: {
         names:'one'
        }
      }
    )
    //取值:this.props.location.state.names
————————————————
版权声明:本文为CSDN博主「the_fool_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/the_fool_/article/details/102602265