Reactjs 我怎样才能通过这个州?

Reactjs 我怎样才能通过这个州?,reactjs,react-native,Reactjs,React Native,如何将此状态传递给父组件 我尝试过使用回调,但由于输出位于文本字段中,因此似乎不起作用。onChangeText无法识别文本字段中的文本何时更改?我也尝试过在Textinput上使用回调,但这似乎也不起作用。也许我在错误的地方设置了状态 这可能吗 Child.js getDLScore(e) { let i; i = deadliftScore.scoreSheet[e]; if (e != '') { if (this.props.mosLevel ===

如何将此状态传递给父组件

我尝试过使用回调,但由于输出位于文本字段中,因此似乎不起作用。onChangeText无法识别文本字段中的文本何时更改?我也尝试过在Textinput上使用回调,但这似乎也不起作用。也许我在错误的地方设置了状态

这可能吗

Child.js

getDLScore(e) {
    let i;
    i = deadliftScore.scoreSheet[e];

    if (e != '') {
      if (this.props.mosLevel === '1') {
        if (e <= 180) {
          return 'fail';
        } else {
          if (this.state.dlPoints != i) {
            this.setState({ dlPoints: i });
          }
          return i;
        }
      }
    }
  }

  render() {
    return (
      <View>
        <View style={styles.eventContainer}>
          <View styles={styles.child2}>
            <Deadlift2
              textChange={dlScoreInput => this.setState({ dlScoreInput })}
            />
          </View>
          <View styles={styles.child3}>
            <Text style={styles.titleName}>Points</Text>
            <Text style={styles.output}>

              {this.getDLScore(this.state.dlScoreInput)} 
              // I want to pass this to my parent component

            </Text>
          </View>
        </View>
      </View>
    );
  }

const Deadlift2 = props => {
  return (
    <View>
      <TextInput
        style={styles.input}
        onChangeText={dlScoreInput => props.textChange(dlScoreInput)}
        value={props.dlScoreInput}
        onKeyPress={props.getDLScore}
      />
    </View>
  );
};
getDLScore(e){
让我;
i=死亡人数分数表[e];
如果(e!=''){
如果(this.props.mosLevel=='1'){
如果(e)
要点
{this.getDLScore(this.state.dlScoreInput)}
//我想把它传递给我的父组件
);
}
const Deadlift2=props=>{
返回(
props.textChange(dlScoreInput)}
value={props.dlScoreInput}
onKeyPress={props.getDLScore}
/>
);
};
Parent.js

render() {
    return (
      <View style={styles.screen2}>
         <Text>Points - {this.state.dlScoreInput}</Text>
      <View>
render(){
返回(
点-{this.state.dlScoreInput}

第一个问题是您忘记将道具
dlScoreInput
传递到
中。
值是
道具。dlScoreInput
,因此添加该属性

<Deadlift2
   textChange={dlScoreInput => this.setState({ dlScoreInput })}
   dlScoreInput={this.state.dlScoreInput}
/>
this.setState({dlScoreInput})}
dlScoreInput={this.state.dlScoreInput}
/>
另一个原因是您没有将更改提交给父级。您可以应用与将信息从
传递回
完全相同的逻辑


但在这一点上,您正在向上传递数据2级。在某一点之后,您最好使用某种类型的全局状态管理系统(如redux)但是如果你坚持在每一步都使用本地状态,我可以告诉你如何编写它。

TextInput
材质ui组件吗?你能链接文档吗?我没有使用react-native,但在常规react中,参数是一个事件对象。不是文本本身。@Andrew不是材质ui的一部分。直接来自react-native。它是我很有兴趣看到传递状态的两种方式,使用本地状态和redux。我只使用react native一周了,所以这对我来说还是很新鲜的。