[英]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:我很想:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.