reactjs - 在卸载时保留反应组件状态
问题描述
在这里反应新手:我注意到组件状态在卸载时被清除。有没有办法防止它?是使用redux吗?
解决方案
你有很多选择。您可以使用状态管理工具,如redux、上下文 API等,也可以将回调传递给父组件并childComponentWillUnmount
像这样触发它:
父组件.jsx:
childComponentWillUnmount = (data) => {
console.log('my data', data);
}
render(){
return <div>
<Child componentUnmountCallback={this.childComponentWillUnmount()}/>
<div>
}
<div>
子.jsx
...
componentWillUnmount() {
this.props.childComponentWillUnmount(this.state);
}
...
推荐阅读
- java - 如何从 Hibernate Jpa 中的父实体仅获取 ChildIds(而不是整个子对象)?
- python - 在 Django 中创建收藏夹列表,新模型或字典数组的最佳选择是什么?
- html - 为什么设置 max-width 会使使用更具体的选择器设置宽度无效?
- ubuntu - 无法统计“b2”:没有这样的文件或目录
- c# - Linq:C# 和 VB.NET 的区别
- typescript - 如何解决 react 本机屏幕打字稿中未定义的 route.params
- anaconda - 橙色折线图小部件的问题
- javascript - 使用 jQuery 和 MVC+WebAPI 项目调用 Web API 的方法是什么
- python - 不了解如何从 tfds 下载 wmt_translate/fr-en 数据集
- python - 使用 MongoDB 处理 JSON 注册表