[英]How can I update the React Native state if I have an array inside an object and the object is inside in a big array?
[英]How can i update state object in react native?
初始 state:
const [question, setQuestion] = useState(response.pathway.question)
上面的 state 我正在使用從 object 得到第一個問題現在同樣的 object 也有答案 object 並且根據答案我需要更改上面的 state 我如何使用 useState 做到這一點。
更改我要完成的 state:
setQuestion(response.pathway.answers[item].question)
當前嵌套 object:
{
"pathway": {
"question ": "this is first question",
"answers": {
"Yes": {
"question": "this question i want to access if user click on yes"
},
"No": {
"question": "this question i want to access if user click on no"
}
}
}
如果我正確理解你的問題。 您想根據當前答案更改問題。 您可以使用 useEffect 掛鈎來完成此操作。 假設你想訂閱可變項的變化。
useEffect(()=>{
setQuestion(response.pathway.answers[item].question);
},[item]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.