简体   繁体   English

我如何在本机反应中更新 state object?

[英]How can i update state object in react native?

Intial state:初始 state:

const [question, setQuestion] = useState(response.pathway.question)

Above state i am using to get the first question from object now same object has answer object also and according to answer i need to change above state how can i do this with useState.上面的 state 我正在使用从 object 得到第一个问题现在同样的 object 也有答案 object 并且根据答案我需要更改上面的 state 我如何使用 useState 做到这一点。

change state that i want to accomplish:更改我要完成的 state:

 setQuestion(response.pathway.answers[item].question)

Current nested object:当前嵌套 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"
    }
}
}

If i'm understanding your question correctly.如果我正确理解你的问题。 You want to change the question according to the current answer.您想根据当前答案更改问题。 You can use the useEffect hook to accomplish that.您可以使用 useEffect 挂钩来完成此操作。 Assuming you want to subscribe to the changes of the variable item.假设你想订阅可变项的变化。

useEffect(()=>{
    setQuestion(response.pathway.answers[item].question);
},[item]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM