【问题标题】:I wanted to convert my react class component to a functional component using react hooks. it gave an error like below我想使用反应钩子将我的反应类组件转换为功能组件。它给出了如下错误
【发布时间】:2020-12-24 05:53:39
【问题描述】:

我想使用反应钩子将我的反应类组件转换为功能组件。它给出了如下错误。

Failed to compile
./src/templates/addPost.js
  Line 6:29:  React Hook "useState" is called in function "addPost" which is neither a React function component or a custom React Hook function  react-hooks/rules-of-hooks

搜索关键字以了解有关每个错误的更多信息。 此错误发生在构建期间,无法消除。

我的代码是:

const AddPost  = (props)=> {
const [post, setPost] = useState({
    id: Math.random(),
    title: '',
    body: ''
})

const handleInput = (target, value) =>{
    return {
        id: post.id,
        title: target.id == 'title' ? value : post.title,
        body: target.id == 'body' ? value : post.body
    }
}

const handleSubmit = (e)=>{
    e.preventDefault()
    console.log(post)
    props.actionPost(post)
    e.target.reset()
    props.history.push('/post/'+ post.id)
}
  

 return ( <form onSubmit={handleSubmit}>
           <input onChange={(e)=>{setPost(handleInput(e.target, e.target.value))}}  type="text" id='title'/>
                            
          <input onChange={(e) => { setPost(handleInput(e.target, e.target.value))}} type="text" id='body'/>
                            
          <button type="submit" onSubmit={handleSubmit}>Submit</button>
         </form>)

}

const mapDispatchToProps = (dispatch) =>{
    return {
        actionPost: (post)=>{
            dispatch(addPostAction(post))
        }
    }
}
export default connect(null, mapDispatchToProps)(AddPost)

【问题讨论】:

  • 在 hooks 中,你不要使用 mapDispatchToProps ,这个视频可以帮助你将类组件更改为函数组件:youtube.com/watch?v=kLCTl1r_dUw
  • 我使用带有钩子的 redux。但它已经解决了,我也可以将 mapDispatchToProps 与功能组件一起使用。

标签: javascript reactjs react-hooks


【解决方案1】:

你只能在 React 组件中使用钩子。通常,创建具有 PascalCase 名称的功能组件是一种很好的风格,并且 Hooks eslint 规则强制执行此操作。

这是来自eslint-plugin-react-hooks 的代码,负责生成该错误消息。

/**
 * Checks if the node is a React component name. React component names must
 * always start with a non-lowercase letter. So `MyComponent` or `_MyComponent`
 * are valid component names for instance.
 */

function isComponentName(node) {
  if (node.type === 'Identifier') {
    return !/^[a-z]/.test(node.name);
  } else {
    return false;
  }
}

因此,Hooks Rule 不会将任何以小写字母开头的函数视为函数式组件。

这种风格的功能组件由 ESLint 在编译时强制执行。这是一个功能组件的示例,它正在执行您正在尝试执行的操作。

export const AddPost = (props) => {
  const [post, setPost] = useState({
    id: Math.random(),
    title: "",
    body: "",
  });

  const handleInput = (target, value) => {
    return {
      id: post.id,
      title: target.id == "title" ? value : post.title,
      body: target.id == "body" ? value : post.body,
    };
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    console.log(post);
    props.actionPost(post);
    e.target.reset();
    props.history.push("/post/" + post.id);
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        onChange={(e) => {
          setPost(handleInput(e.target, e.target.value));
        }}
        type="text"
        id="title"
      />

      <input
        onChange={(e) => {
          setPost(handleInput(e.target, e.target.value));
        }}
        type="text"
        id="body"
      />

      <button type="submit" onSubmit={handleSubmit}>
        Submit
      </button>
    </form>
  );
};

【讨论】:

  • 我利用了这个功能。问题已解决。
【解决方案2】:

如果 addPost 是组件,那么“a”应该是大写字母。每个组件名称都应该以大写字母开头..

const AddPost  = (props)=>

【讨论】:

  • 导出默认 AddPost..您不是从组件导出..请试试这个
  • export default connect(null, mapDispatchToProps)(AddPost)我导出了
猜你喜欢
  • 2020-09-17
  • 1970-01-01
  • 2020-11-30
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2019-12-22
  • 2021-01-16
相关资源
最近更新 更多