簡體   English   中英

this.props.history.push 未定義 state

[英]this.props.history.push has undefined state

我正在嘗試使用 state 參數通過this.history.push將一些數據從我的SignInForm class 發送到另一個組件。

但是,當我查看AdminPage class 時,state 的屬性是undefined

如何使用history.push將數據從一個 class 正確發送到另一個?

登錄表單.js

  .then(response =>{
  const my_user = this.state.user;

  if(response.status === 200){
      this.setState({user : response.data})
    }
      if(this.state.user.roles[0].roleCode === 'ADMIN'){
        this.props.history.location.pathname = "/"; 
        
        // user :{"id":1,"name":"name ","email":"name@gmail.com","roles":[{"id":1,"roleCode":"ADMIN","roleDescription":"restaurant administrattor","authority":"ADMIN"}]}
        console.log("user :" + JSON.stringify(my_user));
        this.props.history.push({pathname: "adminPage", state : { user : my_user}});
      }
      else if(this.state.user.roles[0].roleCode === 'USER'){
        this.props.history.location.pathname = "/";
        this.props.history.push({pathname: "customerPage",state :{ user : my_user}});
      }

  })
  .catch(error => this.setState({error : true,message : "cannot decrypt user info"}))


  })
  .catch(error => this.setState({error : true,message : "cannot decrypt user info"}))

AdminPage.js

constructor(props){ 
    super(props)
    this.state = {
        user : this.props.history.location.state.user
    }
    console.log(this.state.user);
}
    this.props.history.push({pathname: "adminPage", state : { user : my_user}});

變成

 this.props.history.push("/adminPage", { state: user}); 

參考 1 參考 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM