从通过 link 检索到的数据修改 setState
modify a setState from the data retrieved via a link
大家好
我是编程的年轻初学者。我正在尝试将数据从 Link 恢复到另一个组件。除了使用 setState 将恢复的数据放入状态对象之外,一切正常。我想我不知道关于 setState 的一切,但我很迷茫。
我有这个 link 发送数据 "Command_ID" :
<Link to={`/produits/${Command_ID}`} className="item" params={{ Command_ID: {Command_ID}}}>
正如预期的那样,我恢复了这样的数据并且我记得它 "order_id" :
state = {orderId: null};
componentDidMount() {
const order_id = this.props.match.params;
console.log(order_id);
this.setState({orderID: order_id});
console.log(this.state.orderID);
}
我可以在 console.log(order_id) 中看到正确的数字在 link 部分恢复正常 "Command_ID"。但是当我尝试将它与 setState 放在一起并在我的控制台中查看它是否有效时,我只有 this.state.orderID
的未定义值
感谢您的帮助:)
setState
是异步方法,要在设置状态后执行某些操作,您可以将函数作为 setState
的第二个参数传递
this.setState({orderID: order_id}, () => console.log(this.state.orderId));
大家好
我是编程的年轻初学者。我正在尝试将数据从 Link 恢复到另一个组件。除了使用 setState 将恢复的数据放入状态对象之外,一切正常。我想我不知道关于 setState 的一切,但我很迷茫。
我有这个 link 发送数据 "Command_ID" :
<Link to={`/produits/${Command_ID}`} className="item" params={{ Command_ID: {Command_ID}}}>
正如预期的那样,我恢复了这样的数据并且我记得它 "order_id" :
state = {orderId: null};
componentDidMount() {
const order_id = this.props.match.params;
console.log(order_id);
this.setState({orderID: order_id});
console.log(this.state.orderID);
}
我可以在 console.log(order_id) 中看到正确的数字在 link 部分恢复正常 "Command_ID"。但是当我尝试将它与 setState 放在一起并在我的控制台中查看它是否有效时,我只有 this.state.orderID
的未定义值感谢您的帮助:)
setState
是异步方法,要在设置状态后执行某些操作,您可以将函数作为 setState
this.setState({orderID: order_id}, () => console.log(this.state.orderId));