我现在还很陌生,想通过链接传递一个论点。我知道我可以通过使用国家来做到这一点,所以我做到了。
<td>
<Link to={{pathname:"user/"+id, state:{user_url:item.url} }} >{item.name}</Link>
</td>
这将重定向到我的user_detail组件
export default class User_Details extends React.Component {
constructor(props){
super(props);
this.state = {
error : null,
isloading: true,
user_detail : null,
user_url : null
};
if (this.props.location.state){
this.state.user_url =this.props.location.state.user_url
}
}
用户可以通过重定向链接或仅通过URL进入此user_detail组件,如果它通过重定向链接到达那里,则我不必从中获取user_url,因为它已保存在状态中。但是,如果用户使用的链接没有状态,那么我首先必须获取链接。
这有效,但是如果我使用重定向链接,则会不断收到警告错误:
index.js:1446警告:无法在服务器上执行React状态更新 未安装的组件。这是空操作,但表示内存泄漏 在您的应用程序中。要修复,请取消所有订阅并异步 componentWillUnmount方法中的任务。 在用户中(由Context.Consumer创建)
我将如何解决这个问题?
答案 0 :(得分:1)
您不能像这样操纵状态,我建议您像这样在componentDidMount
中使用setState
componentDidMount() {
if (this.props.location.state){
this.setState({
user_url: this.props.location.state.user_url
});
}
}
答案 1 :(得分:0)
就像用户Rishabh Rawat所说的那样,我必须使用componentDidMount函数,在这里我进行了异步调用,但是如果传递了user_link的参数,则不要使用setstate函数设置状态,而是这样做了。state.user_link = this.prop.location.state.user_link。不是SetState,因为这有时会延迟到某些崩溃。