简体   繁体   English

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

[英]Can't call useState hook in arrow function

Im currently trying to implement the tic tac toe example from react but using hooks and ES6 standards but I cant seem to be able to call useState.我目前正在尝试从 react 实现井字游戏示例,但使用钩子和 ES6 标准,但我似乎无法调用 useState。 I keep getting these errors:我不断收到这些错误:

Line 6:33: React Hook "useState" is called in function "game" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks第 6:33 行:在函数“game”中调用 React Hook “useState”,它既不是 React 函数组件,也不是自定义 React Hook 函数 react-hooks/rules-of-hooks

My current versions are:我目前的版本是:
"react": "^16.12.0", “反应”:“^ 16.12.0”,
"react-dom": "^16.12.0", "react-dom": "^16.12.0",
"react-scripts": "3.4.0" “反应脚本”:“3.4.0”

This is my current code:这是我当前的代码:

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">
  );
};

Thanks a lot!非常感谢!

You need to capitalize the component name.您需要将组件名称大写。 Link to the docs链接到文档

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

相关问题 使用useState钩子无法限制功能 - Can't throttle function with useState hook 无法通过 function 调用使用 useState 挂钩设置 state - Unable to set state with useState hook with function call 无法使用 useState 钩子访问 prevState - Can't access prevState with useState hook UseState - 无效的挂钩调用。 Hooks 只能在函数组件内部调用 - UseState - Invalid hook call. Hooks can only be called inside the body of a function component 用 useState 钩子更新对象的 state 后做出反应,我不能调用它的任何函数 - React after updating an object's state with useState hook, I can't call any of its functions 为什么不能将 useState 钩子的 dispatch 函数直接传递给 onClick 处理程序? - Why can't the dispatch function of a useState hook be passed directly into an onClick handler? 使用 React useState hook function 调用设置中止 state - abort state set with React useState hook function call 为什么在超级构造函数的调用中不能在箭头函数中使用“ this”? - Why can't I use “this” in an arrow function in a call to the super constructor? 无法在节点 JS 中从另一个箭头 function 调用箭头 function - Can't call arrow function from another arrow function in Node JS 你能在 onClick function 里面使用 useState 钩子吗? - Can you use useState hook inside onClick function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM