如何在本机反应中从同级兄弟向同级兄弟发送或传递状态?

时间:2019-05-29 10:12:26

标签: javascript reactjs react-native state react-props

我在react native中创建了一个样板。当我有父母并且必须要孩子的组件,并且我想要从孩子1到孩子2粘贴状态

我尝试先发送给父级,然后发送给children2,但不起作用

这是我的代码 父

class WashingPackageMotor extends Component {
render() {
    return (
      <View style={styles.containerMain}>
        <Grid scrollable>
          <Section>
            <Block size="100%">
              <ComponentWashingPackage />
            </Block>
          </Section>
          <Section>
            <Block size="100%">
              <ComponentList />
            </Block>
          </Section>
        </Grid>
        <Grid>
          <Section>
            <Block size="100%">
              <ComponentBottom />
            </Block>
          </Section>
        </Grid>
      </View>
}
    );
  }
}

孩子1(同级一个)

export default class ComponentList extends Component {
    constructor(props) {
        super(props)
        this.state = {
            value: 0,
            amount: 0,
            v0: 0,
            v1: 0,
            collapsed: false
            // v5: 0,
            // v6: 0
        }
        this.amount = 0
    }
..........
..........
}

孩子2(两个兄弟姐妹)

class ComponentBottom extends Component {
  render() {
    return (
      <Grid>
        <View style={styles.bottomView}>
          <View style={{ borderBottomColor: '#EEEEEE', borderBottomWidth:0.5}}/>
          <Section>
            <Block size="60%">
              <Text style={styles.textHarga}>Total Harga</Text>
              <Text style={styles.textPrice}>Rp 180.000</Text>
            </Block>
            <Block size="40%">
              <ButtonProcessDisabled onPress={() => this.props.navigation.navigate('OrderConfirmation')}/>
            </Block>
          </Section>
        </View>
      </Grid>
    )
  }
}

以及如何将状态从子1(兄弟1)发送到子2(兄弟2)

1 个答案:

答案 0 :(得分:0)

获得所需功能的步骤(尽管建议使用redux或context,但不建议使用):

父母:

class WashingPackageMotor extends Component {
    constructor(props) {
        super(props);
        this.state = {
            amount: "",
        }
    }

    update(amount) {
        this.setState({amount});
    }

    render() {
        return (
          <View style={styles.containerMain}>
            <Grid scrollable>
              <Section>
                <Block size="100%">
                  <ComponentWashingPackage />
                </Block>
              </Section>
              <Section>
                <Block size="100%">
                  <ComponentList
                     amount={this.state.amount} 
                     updateAmount={amount => this.update(amount)}
                  />
                </Block>
              </Section>
            </Grid>
            <Grid>
              <Section>
                <Block size="100%">
                  <ComponentBottom 
                    amount={this.state.amount}
                  />
                </Block>
              </Section>
            </Grid>
          </View>
    }
        );
      }
    }

Child1:

只要要更新状态,只需调用函数this.props.updateAmount(valueHere);

export default class ComponentList extends Component {
    constructor(props) {
        super(props)
        this.state = {
            value: 0,
            amount: this.props.amount,
            v0: 0,
            v1: 0,
            collapsed: false
            // v5: 0,
            // v6: 0
        }
        this.amount = 0
    }
..........
..........
}

孩子2:

class ComponentBottom extends Component {
  constructor(props) {
    super(props);
    this.state = {
      amount: this.props.amount,
    };
  }
  render() {
    return (
      <Grid>
        <View style={styles.bottomView}>
          <View style={{ borderBottomColor: '#EEEEEE', borderBottomWidth:0.5}}/>
          <Section>
            <Block size="60%">
              <Text style={styles.textHarga}>Total Harga</Text>
              <Text style={styles.textPrice}>Rp 180.000</Text>
            </Block>
            <Block size="40%">
              <ButtonProcessDisabled onPress={() => this.props.navigation.navigate('OrderConfirmation')}/>
            </Block>
          </Section>
        </View>
      </Grid>
    )
  }
}

注意:对于早于16.3的版本,您将需要使用componentWillReceiveProps(),对于高于16.3的版本,您将需要使用getDerivedStateFromProps根据更新的道具来更新状态。