javascript - 通过链接传递状态
问题描述
我对做出反应还很陌生,并想通过链接传递论点。我知道我可以通过使用 State 来做到这一点,所以我做到了。
<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 创建)
我将如何解决这个问题?
解决方案
你不能像这样操纵状态我建议你componentDidMount
像这样使用 setState
componentDidMount() {
if (this.props.location.state){
this.setState({
user_url: this.props.location.state.user_url
});
}
}
推荐阅读
- java - 使用 Amazon Athena 从 S3 读取多个 json 文件
- javascript - Chart.js - 未捕获的 ReferenceError:未定义图表
- angular - N个相同元素的Formgroup
- c# - 将日期时间声明为 UTC?
- excel - 从另一个工作簿的各个工作表中的单元格中获取值
- python - 使用 Pandas 获取重复的大型 Excel 文件行
- ansible - Ansible 有没有办法让全局任务在所有剧本中使用
- ios - Swift - 在 MapKit 上的多个图像之间切换的有效方法
- node.js - 在 Mocha/Chai/Sinon 中存根 DB 连接线
- git - 如何获取所有 git 提交到合并分支的列表?