【发布时间】:2021-12-27 19:16:29
【问题描述】:
作为调查的一部分,我有一个供用户完成的问题列表,我将问题呈现在不同的组件中,这些组件都位于父文件中。我能够从状态中更新的子组件中获取每个值,但现在想要更新我在当前设置为 Null 的 JSON 对象中拥有的 Answer 字段。
我需要访问每个问题并更新answer 字段。
我的父文件目前如下所示:
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);
/>
);
}
及子组件文件:
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>
);
}
我在我的父组件中得到了答案,并成功运行了 console.log(answer)。
我希望能够在运行 axios 请求提交之前更新 JSON 对象中的答案字段。
我认为在我的 updateQuestion 函数中运行一些类似的东西可能会起作用,但想知道最好的方法吗?
data.questions[type_index].questions[question_index].answer.answer = answer;
提前谢谢你!
【问题讨论】:
标签: reactjs json react-native function state