[英]How to enter questionText from objects inside array inside object
I have tried to enter questionText by inputFields.questions.questionText, but it is not working我试图通过 inputFields.questions.questionText 输入 questionText,但它不起作用
const [inputFields, setInputFields] = useState([ { sectionName: "", sectionDesc: "", questions: [{ questionType: "", questionText: "" }], }, ]); <input value={inputFields.questions.questionText} />
1)
questions
and inputFields
are arrays not an object so you should use index
to access its value as: 1)
questions
和inputFields
是 arrays 不是 object 所以你应该使用index
来访问它的值:
value={inputFields[0].questions[0].questionText}
2)
If you want to change input value on input of value
then you have to use onChange
here as: 2)
如果您想在输入值时更改输入value
,那么您必须在此处使用onChange
作为:
function onChangeInput(e) {
setInputFields((currValue) => {
const clone = [...currValue];
clone[0].questions[0].questionText = e.target.value;
return clone;
});
}
This is an array.这是一个数组。 This is not an object.这不是 object。 Hence, for accessing the value you should use the index of the array:因此,要访问该值,您应该使用数组的索引:
inputFields[index].questions[index].questionText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.