reactjs - React - 我怎样才能在状态下只渲染一次值
问题描述
我需要做的是用一个值设置状态,然后通过道具将数据发送给一个孩子,但我希望“状态”在这样做一次后忘记这一点。
this.setState({
animateId: 15 //or any number of id
});
然后稍后
if(this.state.animateId){
let idToSent = this.state.animateId;
}
<Items <...different props> animate={idToSent}/> //once after settingState - 15,
// then undefined or whatever with every next time
我想取这个值一次,通过 props 发送它,然后不管它,无论哪种方式。
有没有办法做到这一点,除了再次改变状态中的那个值,因为这会导致不必要的渲染?
解决方案
我们可以在任务完成后将其设置为 undefined
this.setState({ doSomething: undefined });
推荐阅读
- java - Android:尽管附加了侦听器,但未调用 onSensorChanged()
- python - 使用 for 循环创建新列表
- php - EntityType 表单返回一个字符串而不是对象
- angular - Sweetalert 没有运行,我的 TS 文件出错了
- pytorch - Pytorch NN 训练问题:NN 的损失并没有减少
- docker - Docker build 找不到 docker 来运行测试
- python - python中带有psycopg2和PostGIS的IF语句
- python - 不和谐机器人通过python添加对消息的反应
- php - Wordpress the_content 显示自定义帖子类型,为什么?我怎样才能删除它?
- azure-data-factory - java.lang.OutOfMemoryError 使用 snappy 压缩将 parquet blob 复制到备份 blob