1

Genere dinamicamente unos componentes de TextInput en ReactNative pero no puedo obtener los valores que el usuario captura, creo que lo mejor seria agregarlos a un array o directamente al state pero no logro hacerlo, intente de varias maneras que me encontre en foros pero sin resultado

export default class Form extends Component {
  constructor(props) {
    //this.changeLoading = this.changeLoading.bind(this)
    super(props);
    this.enviar = this.enviar.bind(this);
    this.state = {
      arr: {
        id: "",
        name: "",
        value: ""
      }
    };
  }

  render() {
    var arreglos2 = [];
    data = this.props.carga.slice();
    return (
      <View style={styles.form_Container}>
        {data.map((objeto, i) => (
          <View key={"n" + i} style={styles.dateD_Info}>
                <View key={"x" + i}>
                  <TextInput
                    style={styles.textbox3}
                    keyboardType="phone-pad"
                    placeholder=""
                    editable={true}
                    maxLength={5}
                    placeholderTextColor="#0891eb"
                    onChangeText={value => console.log(arreglos2.push({id:i,cantidad:value}))}
                    value={arreglos2[i]}
                  />
                </View>
          </View>
        ))}
      </View>
    );
  }
}

los elementos no se guardan y despues no puedo recuperarlos para incrustarlos en un objeto

1 respuesta 1

2

Lo guardas en el state de esta forma

export default class Form extends Component {
  constructor(props) {
    //this.changeLoading = this.changeLoading.bind(this)
    super(props);
    this.enviar = this.enviar.bind(this);
    this.state = {
        textInput: [],
      };
    };
  }

  render() {
    data = this.props.carga.slice();
    const { textInput } = this.state;
    return (
      <View style={styles.form_Container}>
        {data.map((objeto, i) => (
          <View key={"n" + i} style={styles.dateD_Info}>
                <View key={"x" + i}>
                  <TextInput
                    style={styles.textbox3}
                    keyboardType="phone-pad"
                    placeholder=""
                    editable={true}
                    maxLength={5}
                    placeholderTextColor="#0891eb"
                    onChangeText={value =>
                      const newTextInputState = textInput;
                      newTextInputState[i] = value;
                      this.setState({textInput: newTextInputState});
                    }
                    value={textInput[i]}
                  />
                </View>
          </View>
        ))}
      </View>
    );
  }
}
2
  • 1
    le cambie el textInput por this.state.textInput al codigo que me pasaste y funciono genial el 22 abr. 2019 a las 3:08
  • Me alegro que te haya funcionado! el 24 abr. 2019 a las 3:06

Tu Respuesta

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

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