简体   繁体   English

如何在react-native中获得NavigatorIOS的子组件TextInput的值

[英]How to get value of child component TextInput of NavigatorIOS in react-native

I have a NavigatorIOS component that contains a settings view. 我有一个包含设置视图的NavigatorIOS组件。 In that settings view is a list of settings that individually open up their own view when clicked on. 在该设置视图中是一组设置列表,单击这些设置可分别打开自己的视图。 When that setting is clicked on, a back arrow and a right button appear, like so: 单击该设置后,将显示一个后退箭头和一个右按钮,如下所示: 后退箭头和右键

I have a function set for when the right button is pressed called onRightButtonPress that is inside of my navigator's push function, which is called from the settings page to bind the settings page to the child. 我在导航器的push函数内部设置了一个函数,该函数用于在按下右键时将其命名为onRightButtonPress ,该函数从设置页面调用以将设置页面绑定到子项。 My push function looks like this: 我的push功能如下所示:

  navHeight(){
    this.props.navigator.push({
        title: 'Height',
        component: Height,

        rightButtonTitle: 'Save',
        passProps: {
          units: this.state.units
        },
        onRightButtonPress: () => {
          console.warn("hey whats up hello");
          this.props.navigator.pop()}
      })
    }

The Height component that's rendered as a child looks like this: 呈现为子级的Height组件如下所示:

class Height extends Component{
  render(){
    return(
        <View style={styles.settingInput}>
        <Text style={styles.inputRow}>Height: </Text>
          <TextInput ref="heightInput" 
         onChangeText={function(text){
           console.warn(text);
         }
         }/>
          <Text style={styles.inputRowRight}> m</Text>
      </View>
    );    
  }
}

Is there any way to access the value of the TextInput in the Height component when onRightButtonPress is called? 调用onRightButtonPress时,有什么方法可以访问Height组件中TextInput的值? My react-native version is 0.27.2 if that helps. 如果有帮助,我的本机版本为0.27.2。

Edit: I Specifically only want to access the text written inside of the TextInput when the Save button is pressed. 编辑:我特别只想在按下“保存”按钮时访问在TextInput内部编写的文本。 Basically, I don't want any props passed to the child to be updated unless it is pressed. 基本上,除非按下它,否则我不希望传递给孩子的任何道具都得到更新。

Just to clarify, I never used react-native, just reactjs. 为了澄清起见,我从未使用过react-native,仅是reactjs。

Maybe you could pass a function directly to the props and assign it as a callback for the onChangueText of the TextInput component. 也许您可以将函数直接传递给props并将其分配为TextInput组件的onChangueText的回调。

const externalGetInputText() => return this.refs.heightInput.value;


navHeight() {
    this.props.navigator.push({
        title: 'Height',
        component: Height,

        rightButtonTitle: 'Save',
        passProps: {
            units: this.state.units,
            internalGetInputText: externalGetInputText
        },
        onRightButtonPress: () => {
          console.warn("hey whats up hello");
          const text = externalGetInputText();
          console.warn(text);
          this.props.navigator.pop()}
    })
}

class Height extends Component{
    render(){
        return(
            <View style={styles.settingInput}>
                <Text style={styles.inputRow}>Height: </Text>
                <TextInput
                    ref="heightInput" 
                    onChangeText={this.props.internalGetInputText}
                />
                <Text style={styles.inputRowRight}> m</Text>
            </View>
        );
    }
}

Last note, I did't test it. 最后一点,我没有测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM