Home > Software design >  How to enter questionText from objects inside array inside object
How to enter questionText from objects inside array inside object

Time:08-13

I have tried to enter questionText by inputFields.questions.questionText, but it is not working

const [inputFields, setInputFields] = useState([
    {
      sectionName: "",
      sectionDesc: "",
      questions: [{ questionType: "", questionText: "" }],
    },
  ]);

<input value={inputFields.questions.questionText} />

CodePudding user response:

1) questions and inputFields are arrays not an object so you should use index to access its value as:

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:

CODESANDBOX LINK

function onChangeInput(e) {
    setInputFields((currValue) => {
        const clone = [...currValue];
        clone[0].questions[0].questionText = e.target.value;
        return clone;
    });
}

CodePudding user response:

This is an array. This is not an object. Hence, for accessing the value you should use the index of the array:

inputFields[index].questions[index].questionText
  • Related