0

I have a state quiz and another one questionList and the following quiz structure is like this:

const [quiz, setQuiz] = useState({
        title: 'test',
        description: 'test',
        user_id: 'test',
        thumbnail: 'test',
        timer: 'test',
        question: []
    })

And somehow the structure of questionList is:

[{
   id: Math.floor(100000 + Math.random() * 900000),
   question: "data",
   answer1: "data",
   answer2: "data",
   answer3: "data",
   answer4: "data"
},
{
   id: Math.floor(100000 + Math.random() * 900000),
   question: "data",
   answer1: "data",
   answer2: "data",
   answer3: "data",
   answer4: "data"
}
]

So with a function, I'm filling the questionList but what I want to do is also beside that update (or replace) the question: [] with the new array from the questionList in the quiz state.

Here is how I add a question in the List:

const addQuestion = (question) => {
   setQuestionList([...questionList, question]);   
   // here the logic to update the property question on Quiz state with the new added object into questionList
};

So when a question is added to the list, that list array I want to update to questions: [], as an update, and not add another field.

5 Answers 5

1

You can use useEffect hook with questionList state. And when questionList updated, this hook triggered and updates quiz state.

React.useEffect(()=>{
  setQuiz({...quiz, question: questionList})
}, [questionList]);
Sign up to request clarification or add additional context in comments.

Comments

1

So basically what i understand from your question .when new question added it should add in your questionList array. so in react we cannot directly mutate states. [1]: https://medium.com/@kkranthi438/dont-mutate-state-in-react-6b25d5e06f42 So in order to solve this we can

  1. Create a new dummy object.

    const addQuestion = (question) => {
    let dummyObj={};//set all your properties here inside your question array also
    
  2. copy existing array into dummy array

    let dummyArr=[...existing array];
    
  3. push object or other values in this array

    dummmyArr.push(dummyObj);//push only work here not for pushing into setState
    
  4. now put this mutated array into your setState .

    setQuestionList(dummyArr)
    };
    

now this method is also suitable if you want to set nested data like youyr question array.

Comments

1

Another solution you can use useEffect for that purpose 1- use Dependency array of useEffect .Every time state of question list change .this will automatically trigger the useEffect.

1 Comment

yes thats a way too
0

you can do this in your function, replace your old function,

const addQuestion = (question) => {
   setQuestionList([...questionList, question]); 
};

to this, this will work

const addQuestion = (question) => {
   setQuestionList([...questionList, question]);   
   setQuiz({...quiz, question: [...questionList, question]})
};

Comments

0

Yeah, you're almost there, just need to update your questionList first and then quiz.

Notice the callback style of setting state.

const addQuestion = (question) => {
   setQuestionList(prevQuestionList => [...prevQuestionList, question]);   
};

and later add reaction to questionList changing

useEffect(() => {
   setQuiz(prevQuiz => ({...prevQuiz, question: questionList}))
}, [questionList])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.