【问题标题】:useState error, i cannot find the solution [duplicate]useState 错误,我找不到解决方案 [重复]
【发布时间】:2021-07-23 11:02:52
【问题描述】:

你能解释一下为什么这段代码是错误的

import React, { useState } from 'react';

import UsrInput from '../component/UsrInput'
import TodoItemList from '../component/TodoItemList'

const todos = () => {

  const DUMMY_TODO_ITEM = [
    {
      id: 'ti1',
      description: 'do programming'
    },
    {
      id: 'ti2',
      description: 'do dishes'
    },
    {
      id: 'ti3',
      description: 'cleaning the house'
    },
  ]

  const [todoItem, setTodoItem] = useState(DUMMY_TODO_ITEM);

  return (
    <React.Fragment>
      <UsrInput />
      <TodoItemList item={DUMMY_TODO_ITEM} />
    </React.Fragment>
  );
}

export default todos;

我收到此错误>>

"编译失败。

src\main\pages\Todos.js 第 23:35 行:在既不是 React 的函数“todos”中调用了 React Hook “useState” 函数组件也不是自定义的 React Hook 函数。 React 组件名称必须以 一个大写字母 react-hooks/rules-of-hooks

搜索关键字以了解有关每个错误的更多信息。”

【问题讨论】:

  • 就像错误信息说的那样,“React 组件名称必须以大写字母开头”。

标签: javascript reactjs


【解决方案1】:

React Components 命名约定说第一个字母应该是大写的。 React Hooks 只能在 React 组件中使用。 所以你必须重命名你的 const todos -> const Todos 用于 react 将其视为组件。

【讨论】:

  • 天哪,谢谢,我太糟糕了
【解决方案2】:

尝试将 'todos' 大写,例如 Todos

在React中,组件需要大写,自定义钩子需要以use开头。

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 2012-06-19
    • 2020-09-03
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多