调用useState时的React Hooks问题

时间:2019-04-30 18:50:19

标签: reactjs

我有以下React Todo组件:

import React, { useState } from 'react';

const todo = props => {

  const inputState = useState('') // problem with this line 

  return (
    <React.Fragment>
      <input type="text" />
    </React.Fragment>
  )
}

export default todo

运行应用程序时,在浏览器中出现以下错误:

./src/Todo.js
  Line 5:  React Hook "useState" is called in function "todo" which is neither a React function component or a custom React Hook function  react-hooks/rules-of-hooks

我正在使用create-react-app来构建我的应用程序,并且正在使用以下版本的React。

  "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.0"

1 个答案:

答案 0 :(得分:2)

您需要在组件名称上写上首字母大写,这样才能将其视为组件。

import React, { useState } from 'react';

const Todo = props => {

  const inputState = useState('') // problem with this line 

  return (
    <React.Fragment>
      <input type="text" />
    </React.Fragment>
  )
}

export default Todo