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

190
Views
Updating Parent component state from Child component TextInput in React Native

I have a list of survey questions as components for a user to fill out and am trying to go through each one and update the parent components state when the child components input field is filled out, however on the first component I keep on receiving the text from the TextInput as undefined.

This is my Parent component '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}
          />
       );
}

And my Child component '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>
  );
}


I've stripped out some styles and imports to keep it simple, when entering text on the BasicTextInput, the console log I get in my parent is 'Undefined'

I imagine I'm missing something simple but any help would be very appreciated! I have a number of these to do so want to get this initial component correct.

Thanks in advance.

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

0

Problem is in below line

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

Here BasicTextInput is component and it receives single argument as props so if you want to restructure it will be

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

0

I was able to accomplish this in the end by passing in an onChangeValue as a prop which takes the newValue and sets state to that newValue.

Here are both updated files as an example:

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

And my Child component file:


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

I hope this helps some of you in future!

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!