【发布时间】:2021-09-20 14:59:03
【问题描述】:
所以我正在使用 react-router,并且在我通过从 page1 到 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>;
当我进入第2页时,我如何更新这个状态,我试过使用this.setState,但这不起作用,我试过像下面那样解构发送过来的状态,但也没有用
constructor() {
super();
this.state = {
title,
price,
desc,
img,
smoking,
guests,
featured,
} = this.props.location.state.resort;
}
有什么建议吗?
【问题讨论】:
-
您是否登录
this.props.location.state以确保您获得状态?你绝对应该使用setState。 -
是的,我正在接收状态,我只是不知道如何更新它@Ace
-
你能说明你是如何使用
setState的吗? -
this.setState({featured: !this.props.location.state.resort.featured}) @Ace
标签: javascript reactjs react-router state routerlink