简体   繁体   English

在 React Native Input/TextInput 中输入值时如何更新 JSON 对象值

[英]How to update a JSON object value when value is entered in React Native Input/TextInput

I have a list questions for a user to complete as part of a survey, I am rendering the Questions out in different components that all live in a parent file.作为调查的一部分,我有一个供用户完成的问题列表,我将这些问题呈现在所有位于父文件中的不同组件中。 I am able to get each value from the child component updated in the state but now want to update the Answer field that I have within the JSON object which is currently set to Null.我能够从状态中更新的子组件中获取每个值,但现在想要更新我在当前设置为 Null 的 JSON 对象中的 Answer 字段。

This is my JSON object called data这是我的名为data JSON 对象在此处输入图片说明

I need to access each question and update the answer field.我需要访问每个问题并更新answer字段。

My parent file currently looks like this:我的父文件目前看起来像这样:

export default function SurveyScreen({navigation, route}) {
  const [childNameValue, setChildNameValue] = useState('');

const updateQuestion = (answer, type_index, question_index) => {
    console.log(answer);
  };

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

And child component file:和子组件文件:

export default function BasicTextInput({title, onChangeValue, updateQuestion}) {
  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}
          updateQuestion={updateQuestion}
        />
        <View />
      </View>
    </View>
  );
}

I'm getting the answer back in my parent component and successfully running console.log(answer).我在我的父组件中得到了答案并成功运行了 console.log(answer)。

I want to be able to update the answer field in the JSON object before running an axios request to submit it.我希望能够在运行 axios 请求以提交它之前更新 JSON 对象中的答案字段。

I thought running something along the lines of this inside my updateQuestion function may work but wondered the best way to do this?我认为在我的 updateQuestion 函数中运行类似的东西可能会起作用,但想知道最好的方法是什么?

data.questions[type_index].questions[question_index].answer.answer = answer;

Thank you in advance!先感谢您!

You could certainly just mutate the field you want to update.您当然可以改变要更新的字段。

Or you could use a proper immutable update that React will be able to detect:或者,您可以使用 React 能够检测到的适当的不可变更新:

// something like
const replace = (arr, i, insert) => [...arr.slice(0, i), insert, ...arr.slice(i+1)];
const updateDeepDeepAnswer = {
  ...data,
  questions: replace(data.questions, type_index, {
  ... data.questions[type_index],
  questions: replace(data.questions[type_index].questions, question_index, {
    ...data.questions[type_index].questions[question_index],
    answer: {
      ...data.questions[type_index].questions[question_index].answer,
      answer,
    },
  }),
}),
}

Which is why we try to keep data as simple as possible in React state.这就是为什么我们试图在 React 状态下尽可能保持数据简单。

I would be tempted to either:我很想:

  1. Store the question sets and the questions on separate state slices.将问题集和问题存储在单独的状态切片上。 So an update to the answer field is not a deep update.所以对答案字段的更新不是深度更新。
  2. Store the answers separately, along with their question id.单独存储答案及其问题 ID。 Then send those questionId + answer objects to your api to be saved in the db.然后将这些 questionId + answer 对象发送到您的 api 以保存在数据库中。

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

相关问题 如何在React Native中更改TextInput中的默认值并使用新值更新 - How to change default value and update with new value in TextInput in react native 在React Native中输入数字值时如何同步和增加文本输入中的值 - How to sync & increment a value within a Text Input when a numeric value is entered in React Native react native如何改变TextInput的值 - How to change the value of TextInput in react native 如何编辑TextInput React native中的值? - How to edit value in the TextInput React native? 如何在 react-native 中获取 TextInput 值 - How to get the TextInput value in react-native React Native TextInput具有默认值当状态更改(onchangetext)时,如何更新/传递DefualtValue作为文本条目 - React Native TextInput have a Default Value How can we update/pass DefualtValue as a Text entry when state change (onchangetext) React Native:如何在 React Native 中发送 TextInput 的值? - React Native: how to send value of TextInput in React Native? React-Native - TextInput - 使默认值看起来像输入的值 - React-Native - TextInput - Make default value look like an entered value 在 React 中更新一个 JSON Object 的值当 onClick - Update a JSON Object Value When onClick in React 使用 textInput 和函数组件在 react native 中更改数组对象值 - Change array object value in react native with textInput and function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM