【发布时间】:2021-05-26 12:32:59
【问题描述】:
我有一个对象myObj,它是data 对象的一个字段。我使用useState 钩子来启动这个对象的状态变量。
myObj 就像{"name": "John", "age": 20, ...}。
import React, {useState} from 'react';
const myComponent = ({data}) => {
const[student, setStudent] = useState(data.myObj); // myObj is a object with value
console.log(`${JSON.stringfy(student)}`); // it prints undefined, why?
return (
// here I need to render the student information but it is undefined.
)
}
我在这里误用了useState 钩子吗?如果是这样,我怎样才能在 useState 钩子之后立即反映初始对象值,就像上面的控制台日志中显示的那样?
【问题讨论】:
标签: react-native react-hooks use-state