React: reading data passed as parameter in history.push

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 15:18:50

Okay,after struggling for 4 days and twitching around the answers given by Luna and JupiterAmy, here is what worked for me:

inside the Login.jsx

this.props.history.push({
          pathname : '/Taskactive',
          state :{
          role_id : responseJson.userFormat,
          userName : this.userName,
          userid: this.refs.usrname.value
          }
          } 
        );

and in Taskactive.jsx

this.props.location.state.role_id

Hope this could help somebody in future.

change this

this.props.history.push(
          '/Taskactive',
          {
            role_id : this.userFormat,
            userName : this.userName
          }
        );

to this:

this.props.history.push({
          pathname: '/Taskactive',
          appState: {
            role_id : this.userFormat,
            userName : this.userName
          }
        });

And inside the component which you are rendering for path /Taskactive, you can access the values as this.props.location.appState.role_id or this.props.location.appState.userName You can also change the name of Object(which I have kept as state) to your wish.

Hope this helps.

if its a component rendered with react-router, the stuff passed are inside component props.

console.log(this.props.match.params)

from what I see, your router configuration is wrong. Login should be a child of <Router>, and shouldn't container router at all.

Example config:

<Router>
  <Switch>
    <Route exact path="/" component={Login} />
    <Route path="/login" component={Login} />
    <Route path="/signup" component={Signup} />
    <Route path="/restore-password" component={RestorePass} />
    <Route path="/forgot-password" component={ForgotPass} />
  </Switch>
</Router>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!