【问题标题】:How to send or passing state from siblings to siblings in react native?如何在本机反应中将状态从兄弟姐妹发送或传递给兄弟姐妹?
【发布时间】:2019-05-29 10:12:25
【问题描述】:

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

我尝试发送给父母然后是孩子2,但没有工作

这是我的代码 家长

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)

【问题讨论】:

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


    【解决方案1】:

    获得你想要的功能的步骤(虽然不建议使用 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>
        }
            );
          }
        }
    

    孩子1:

    只要你想更新状态,只需调用函数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 的 react 版本,您需要使用 componentWillReceiveProps(),对于以上版本,您需要使用 getDerivedStateFromProps 根据更新后的 props 更新状态。

    【讨论】:

    • 如果我想发送状态 v0:0 我必须做什么,当我想发送 v0:0 时你能告诉我详细信息吗?谢谢
    • 只需在父级中使用与金额相同的状态,并在父级中创建另一个函数来更新它。也可以通过 props 传递给子组件。
    猜你喜欢
    • 2021-11-19
    • 2019-05-06
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2014-07-12
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多