简体   繁体   English

从 React Native 中的 TextInput 组件获取值

[英]Get value from a TextInput component in react native

Dynamically generate a TextInput when you press a button but I can't get the value that the user digits,try to use states but I can't because it's not with the other general textInputs but it's imported as Field.当您按下按钮时动态生成一个 TextInput 但我无法获得用户数字的值,尝试使用状态但我不能,因为它不与其他通用 textInputs 一起但它作为字段导入。

try to create a state in the component file and move it to the general view and print it to see if it works and not...is there any way to bring this state?尝试在组件文件中创建一个 state 并将其移动到一般视图并打印它以查看它是否有效,而不是...有什么办法可以带来这个 state?

general view:普遍的看法:

    import Campo from './campoInput';
  
constructor(props){
super(props);

this.state={
  Cbusto:"",
  Ccintura:"",
  Ccadera:"",
  valueArray: []
};
this.addNewEle = false;

}

  agregarCampo=()=>{
  this.addNewEle = true;
  const newlyAddedValue = { text: 'prueba'};

  this.setState({
  
    valueArray: [...this.state.valueArray, newlyAddedValue]
  });
}
render(){
return(

------Here are the normal textInput-----

     <View style={{ flex: 1, padding: 4 }}>
        {this.state.valueArray.map((ele) => {
          return <Campo item={ele} />;
        })}
      </View>

      <View style={styles.flex}>
        <View style={styles.ButtonAdd}>
          <Button
            title="Add input"
            color="#B13682"
            onPress={this.agregarCampo}
          ></Button>
        </View>

)
}

Component:零件:

constructor(props){
    super(props);
    
     this.state={
    info:""
    }; 
    }

render(){
    return(
        <View>
            <Text>pruba:{this.props.item.text}</Text>
            <View style={styles.input}>
            <TextInput onChangeText={(text) => this.setState({info:text})}></TextInput>
            </View>
        </View>
    )
}

can be solved by adding the onChangeText event to the Field component in the overview and in the same way in the TextInput of the component being imported, using props status可以通过将 onChangeText 事件添加到概览中的 Field 组件中来解决,并且以相同的方式在正在导入的组件的 TextInput 中,使用道具状态

General view:普遍的看法:

<View style={{ flex: 1, padding: 4 }}>
        {this.state.valueArray.map((ele) => {
          return <Campo item={ele}
          onChangeText={(text) => this.setState({ info: text })}
          />;
        })}
 </View> 

Component零件

 <View>
         <Text>pruba:{this.props.item.text}</Text>
         <View style={styles.input}>
         <TextInput onChangeText={this.props.onChangeText}></TextInput>
         </View>
 </View>

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

相关问题 如何在react-native中获得NavigatorIOS的子组件TextInput的值 - How to get value of child component TextInput of NavigatorIOS in react-native React Native:从<textinput>进入在组件外部声明的 function</textinput> - React Native: Passing value from <TextInput> into a function declared outside of a component 如何从 TextInput 获取值并使用 Button React Native 进行设置? - How to get value from TextInput and set it with Button React Native? 从TextInput获取值并将其传递给祖父母,并传递给子代反应本机 - get value from TextInput and pass to grandparent and to the child react native 在React native中获取TextInput值和复选框状态 - Get TextInput Value and Checkbox state in React native 如何在 react-native 中获取 TextInput 值 - How to get the TextInput value in react-native 在 React Native 中从子组件 TextInput 更新父组件状态 - Updating Parent component state from Child component TextInput in React Native 反应本机多行,值,小于TextInput组件中的占位符 - React Native multiline, value, smaller than placeholder in TextInput component 使用 textInput 和函数组件在 react native 中更改数组对象值 - Change array object value in react native with textInput and function component 如何在 React Native 的 TextInput 组件中获取动态引用和焦点 - How to get dynamic ref and focus in TextInput component on React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM