reactjs - React-native ComponentDidMount 未触发
问题描述
每次更改时,我都尝试更新从其他组件导入的声音数组。但是,它只会触发一次 componentDidMount() 并且不会再次运行。下面是我关于这个问题的代码:
//sound array from another component
import { soundArray } from "./CreateRecord";
export default class RecordingList extends React.Component {
constructor() {
super();
this.currentSoundArray = [];
}
componentDidMount() {
console.log(this.currentSoundArray);
this.getCurrentArray();
}
getCurrentArray() {
this.currentSoundArray = soundArray;
}
render(){
...
}
目前,当我查看组件时,componentDidMound 将运行一次并控制声音数组。起初,声音数组是空的:
[]
但是,在我将值放入声音数组并返回查看组件后,它不会打印控制台,也不会更新 this.currentSoundArray 的值
我的预期结果应该是 currentSoundArray 每次在另一个组件中更改 soundArray 时都会更改并打印到控制台。例如:
[]
[1,2]
[1,2,4]
解决方案
componentDidMount() 在组件安装后立即调用(插入到树中)。需要 DOM 节点的初始化应该放在这里。如果您需要从远程端点加载数据,这是实例化网络请求的好地方。
它只运行一次。
推荐阅读
- android - 如何在recyclerview的顶部显示房间数据库中的最后一项
- c++ - 在 Linux 上的 gcc 中编译后更改符号和对象的可见性
- vue.js - Vue-Multi-Select 获取数据数组
- node.js - 无法通过node js发送电子邮件,尝试了几种方法
- python - python - 如何在一个参数中为python中的散射函数设置颜色和标记
- c - 任何人都可以详细解释以下c中的指针程序吗?
- php - 将字符串格式存储在db中的数据转换为对象日期时间
- apache - 导入 .war 文件时出现 Apache 错误。“错误:失败 - 无法启动上下文路径 [/文件夹] 处的应用程序”
- java - 如何转换复杂对象列表并将其表示为值 yaml 图表
- javascript - 在Javascript中使用Symbol.iterable进行迭代时如何检测当前元素是最后一个元素?