0

Genere dinamicamente unos TextInput cuando se presiona un boton pero no puedo obtener el valor que el usuario digita,intente usar estados pero no lo logro ya que no esta con los otros textInputs generales si no que se importa como Campo.

intente crear un estado en el archivo del componente y pasarlo a la vista general e imprimirlo para ver si funciona y no...hay alguna forma de traer este estado?

vista general:

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(

------aca estan los otrox textInput normales-----

     <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>

)
}

componente:

   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>
    )
}
 

1 respuesta 1

0

se logro solucionar agregando el evento onChangeText a el componente Campo en la vista general y de la misma forma en el TextInput del componente que se importa, mediante estado props:

Vista general:

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

Componente:

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

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.