简体   繁体   English

在 React Native 中从子组件 TextInput 更新父组件状态

[英]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.我有一个调查问题列表,作为供用户填写的组件,我正在尝试检查每个问题并在填写子组件输入字段时更新父组件状态,但是在第一个组件上我一直在接收文本来自未定义的 TextInput。

This is my Parent component 'SurveyScreen':这是我的父组件“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':我的子组件“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'我删除了一些样式和导入以保持简单,在 BasicTextInput 上输入文本时,我在父级中获得的控制台日志是“未定义”

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.提前致谢。

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这里 BasicTextInput 是组件,它接收单个参数作为道具,所以如果你想重组它,它将是

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

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.最后,我能够通过传入一个 onChangeValue 作为道具来实现这一点,该道具采用 newValue 并将状态设置为该 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!我希望这对你们中的一些人有帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM