Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Actualización del estado del componente principal desde el componente secundario TextInput en React Native

Tengo una lista de preguntas de la encuesta como componentes para que un usuario las complete y estoy tratando de revisar cada una y actualizar el estado de los componentes principales cuando se completa el campo de entrada de los componentes secundarios, sin embargo, en el primer componente sigo recibiendo el texto. de TextInput como indefinido.

Este es mi componente principal 'SurveyScreen':

 export default function SurveyScreen({navigation, route}) { const [childNameValue, setChildNameValue] = useState(''); function handleChange(newValue) { setChildNameValue(newValue); console.log(childNameValue); } return ( <ScrollView style={styles.cardContainer}> <View> <Text style={styles.title}>Please fill out all questions below</Text> </View> <View> <BasicTextInput title="Child's full name" value={childNameValue} onChange={handleChange} /> ); }

Y mi componente secundario 'BasicTextInput':

 import React, {useState} from 'react'; import { View, Text, StyleSheet, Pressable, Platform, TextInput, } from 'react-native'; import CheckBox from '@react-native-community/checkbox'; export default function BasicTextInput({title, onChange}, props) { const [text, onChangeText] = useState(''); function handleChange(e) { onChange(e.target.value); } return ( <View> <View> <Text>title</Text> </View> <View> <TextInput style={styles.input} onChange={handleChange} value={props.childNameValue} /> <View /> </View> </View> ); }

Eliminé algunos estilos e importaciones para mantenerlo simple, al ingresar texto en BasicTextInput, el registro de la consola que obtengo en mi padre es 'Indefinido'

Me imagino que me estoy perdiendo algo simple, ¡pero cualquier ayuda sería muy apreciada! Tengo varios de estos para hacerlo, así que quiero que este componente inicial sea correcto.

Gracias por adelantado.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El problema está en la línea de abajo.

 export default function BasicTextInput({title, onChange}, props) {

Aquí BasicTextInput es un componente y recibe un solo argumento como accesorios, por lo que si desea reestructurarlo, será

 export default function BasicTextInput({title, onChange}) {
about 4 years ago · Juan Pablo Isaza Report

0

Pude lograr esto al final al pasar onChangeValue como accesorio que toma el valor nuevo y establece el estado en ese valor nuevo.

Aquí están ambos archivos actualizados como ejemplo:

 export default function SurveyScreen({navigation, route}) { const [childNameValue, setChildNameValue] = useState(''); return ( <ScrollView style={styles.cardContainer}> <View> <Text style={styles.title}>Please fill out all questions below</Text> </View> <View> <BasicTextInput title="Child's full name" value={childNameValue} onChangeValue={newValue => setChildNameValue(newValue)} /> ); }

Y mi archivo de componente Child:

 export default function BasicTextInput({title, onChangeValue}) { return ( <View style={styles.container}> <View style={styles.containerHeader}> <Text style={styles.questionTitle}>{title}</Text> </View> <View style={styles.answerContainer}> <TextInput style={styles.input} onChangeText={onChangeValue} /> <View /> </View> </View> ); }

¡Espero que esto ayude a algunos de ustedes en el futuro!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!