所以我正在使用 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
有什么建议吗?
答案 0 :(得分:3)
永远不要直接改变 this.state
试试这个:
constructor() {
super();
this.state = { resort: {} };
}
componentDidMount = () => {
this.setState({resort: this.props.location.state.resort});
}
答案 1 :(得分:1)
考虑使用 componentDidMount
生命周期钩子并使用 setState
将这些值设置为组件 state
像这样
像这样
constructor() {
super();
this.state = {
resort: {}
}
}
compoonentDidMount() {
this.setState({ resort: this.props.location.state.resort })
}