How to pass params with history.push/Link/Redirect in react-router v4?

前端 未结 10 1179
你的背包
你的背包 2020-11-22 04:27

How can we pass parameter with this.props.history.push(\'/page\') in React-Router v4?

.then(response => {
       var r = this;
        if (re         


        
10条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 04:54

    • For the earlier versions:

      history.push('/path', yourData);
      

      And get the data in the related component just like below:

      this.props.location.state // it is equal to yourData
      
    • For the newer versions the above way works well but there is a new way:

      history.push({
        pathname: '/path',
        customNameData: yourData,
      });
      

      And get the data in the related component just like below:

      this.props.location.customNameData // it is equal to yourData
      

    Hint: the state key name was used in the earlier versions and for newer versions, you can use your custom name to pass data and using state name is not essential.

提交回复
热议问题