无法在箭头函数中调用useState钩子

时间:2020-02-23 09:30:08

标签: javascript reactjs react-hooks

我目前正在尝试从react实现井字游戏示例,但使用了hook和ES6标准,但我似乎无法调用useState。我不断收到这些错误:

第6:33行:在函数“游戏”中调用React Hook“ useState”,该函数既不是React函数组件也不是自定义的React Hook函数react-hooks / rules-of-hooks

我当前的版本是:
“反应”:“ ^ 16.12.0”,
“ react-dom”:“ ^ 16.12.0”,
“反应脚本”:“ 3.4.0”

这是我当前的代码:

import React, { useState } from "react";
import Restart from "../../Components/Restart/Restart";
import Square from "../../Components/Square/Square";

const game = () => {
  const [squares, setSquares] = useState(Array(9).fill(null));
  const [isXNext, setIsXNext] = useState(true);
  const nextSymbol = isXNext ? "X" : "O";
  const winner = calculateWinner(squares);
  ...
  return (
    <div className="container">
  );
};

非常感谢!

1 个答案:

答案 0 :(得分:6)

您需要大写组件名称。链接到docs