首页 > 解决方案 > 如何将状态从父视图传递给子视图?

问题描述

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>
    )
  }

标签: react-native

解决方案


而是传递完整的状态(不推荐)

将值作为道具传递,例如

<ParentComponent>
      <ChildComponent dataOne={this.state.stateOfDataOne}/>
</ParentComponent>

然后在您的子组件中

<ChildComponent>
        <Text>{this.props.dataOne}</Text>
</ChildComponent>

推荐阅读