react-native - 如何将状态从父视图传递给子视图?
问题描述
MyPinchZoomView 有一个状态“比例”。在下面的设置中,组件“板”如何跟踪当前比例?
render() {
return (
<MyPinchZoomView
minScale={0.9}
maxScale={2}
>
{ this.board }
</MyPinchZoomView>
)
}
同样的问题,如果我要在板组件中添加 MyPinchZoomView:
return (
<View>
<MyPinchZoomView
minScale={0.9}
maxScale={2}
>
{ this.fields }
{ this.rackFields }
{ this.figures }
</MyPinchZoomView>
<Toolbar board={this} figures={this.figures}></Toolbar>
<Text>{this.state.lastRefresh}</Text>
<Moves game={this.props.game} lastRefresh={this.state.lastRefresh}/>
<FlashMessage position="top" ref="errorMessage" />
</View>
)
}
解决方案
而是传递完整的状态(不推荐)
将值作为道具传递,例如
<ParentComponent>
<ChildComponent dataOne={this.state.stateOfDataOne}/>
</ParentComponent>
然后在您的子组件中
<ChildComponent>
<Text>{this.props.dataOne}</Text>
</ChildComponent>
推荐阅读
- maven - 存储库版本策略:忽略丢失的 pom?
- jquery - jQuery click() 只有一次
- javascript - 加载新的 html 页面时无法使用 javascript 变量
- bluetooth - 交换前 bluez 配对
- bash - 使用 RegEx 或 Bash 脚本重构
- database-design - 如何在不创建额外表的情况下存储冗余数据?
- javascript - JavaScript/React Native 数组(对象)排序
- node.js - 2 Way SSL 证书的 CN 名称
- python - 为什么高级索引会创建切片矩阵的副本?
- java - 获取 JSON 字符串的抽象语法树