I am new to react-native and i want to update data in objects array! Here is the array :
const questions = [
{
question: "What kind of fruit was used to name a computer in 1945?",
answers: [
{ id: "0", text: "192.168.1.1", correct: false, userInput: false },
{ id: "1", text: "127.0.0.1", correct: true, userInput: false },
{ id: "2", text: "209.85.231.104", correct: false, userInput: false },
{ id: "3", text: "66.220.149.25", correct: false, userInput: false },
],
},
{
question: "What kind of fruit was used to name a computer in 1984?",
answers: [
{ id: "0", text: "Blackberry1", correct: false, userInput: false },
{ id: "1", text: "Blueberry", correct: false, userInput: false },
{ id: "2", text: "Pear", correct: false, userInput: false },
{ id: "3", text: "Apple", correct: true, userInput: false },
],
},
];
It is random text. I want to update userInput (when i press a button) value of the first answers object like -
questions[0].answers[0].userInput = true;
Thats of courst not works without hooks so i change const questions with
const [questions, setQuestions] = useState([here i paste object array above])
And i wrote following code to check if value changed when i press a button. So code in the button onPress event is:
questions[0].answers[0].userInput = true;
setQuestions([questions]);
console.log(questions); // to check outeput
Nothing of course happened and this object (questions[0].answers[0].userInput = true;
) is not changed
So how can i fix that? I think i should create copy of the array and then changed this value. I saw this lib that can help me but i dont know how to start?