如何解决如何更新通过反应路由器链接传递的状态
所以我正在使用 react-router,并且在我从 page1 到 page2 发送一些带有链接的状态后
chart.generateLegend()
当我进入 page2 时,如何更新此状态,我已尝试使用 <Link
to={{
pathname: `/dashboard/edit/${resort.id}`,state: { resort: resort },}}
style={{ textdecoration: 'none',color: 'black' }}
>
<i
className="fas fa-edit db-icon"
style={{ background: 'rgb(12,177,12)',color: 'black' }}
></i>
</Link>;
,但这不起作用,我已尝试解构如下发送的状态,但没有用要么
this.setState
有什么建议吗?
解决方法
永远不要直接改变 this.state
试试这个:
constructor() {
super();
this.state = { resort: {} };
}
componentDidMount = () => {
this.setState({resort: this.props.location.state.resort});
}
,
考虑使用 componentDidMount
生命周期钩子并使用 setState
将这些值设置为组件 state
像这样
像这样
constructor() {
super();
this.state = {
resort: {}
}
}
compoonentDidMount() {
this.setState({ resort: this.props.location.state.resort })
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。