reactjs - react-native 中的实时条件渲染
问题描述
我试图弄清楚如何基于布尔值渲染组件。但是用户也可以选择更改布尔值,我需要应用程序实时更新。
为简单起见,目前我只尝试渲染一些文本:
const deposit = <Text>deposit</Text>
const noDeposit = <Text>noDeposit</Text>
这是我想要渲染它的视图:
<View>
{this.props.depositRequired ? deposit : noDeposit}
</View>
它适用于在应用程序首次启动时显示其中一个。但它永远不会更新,我有点不知道如何做到这一点。任何帮助深表感谢!
解决方案
尝试使用生命周期方法来接收调用的道具componentWillReceiveProps
以进行组件更新。像这样实现它:
componentWillReceiveProps(nextProps){
if(nextProps.depositRequired !== this.props.depositRequired){
// just call to update the component
this.setState({})
}
}
推荐阅读
- git - 从现有存储库创建新存储库
- javascript - jQuery 在点击功能时动态更改图标
- kubernetes - 无法访问部署在 kubernetes 集群上的应用程序
- ffmpeg - FFmpeg 混合模式不适用于“乘法”选项
- java - 使用 h2o rest api 获取模型信息时总是出错
- javafx - javafx中的高CPU使用率
- python - 使用循环 For 和 If 在文件中查找字符串
- json - 使用 jq 将 bash 变量插入 json
- css - 自定义工具提示 Bootstrap 4
- dsl - 如何重试请求,直到我使用空手道 dsl 获得有效的动态生成值作为响应