我试图在 React hooks 中提取价值,但与此同时,当我控制台时,customer我收到此错误TypeError: Cannot read property 'firstName' of null,我不知道我的代码中有什么问题。我是 React Hook 的新手,有人可以帮我解决这个问题吗?
谢谢
当我安慰客户时,我得到这个结果
当我安慰 customer.firstName 时
它给我错误
代码
const [customer, setCustomer] = useState(null);
async function fetchMyAPI() {
let response = await fetch(
`/api/requirements/find?customerId=${item.customerId}`
);
response = await response.json();
console.log(response);
setCustomer(response);
}
useEffect(async () => {
fetchMyAPI();
}, []);
Run Code Online (Sandbox Code Playgroud)
在返回函数中
{console.log(customer.firstName)}
Run Code Online (Sandbox Code Playgroud)