小编jon*_*nny的帖子

React Hooks:类型错误:无法读取 null 的属性“firstName”

我试图在 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)

javascript ecmascript-6 reactjs react-hooks

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

ecmascript-6 ×1

javascript ×1

react-hooks ×1

reactjs ×1