【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)
  }

猜你喜欢

转载自www.cnblogs.com/the-fool/p/11365425.html