【问题标题】:Error: Hooks can only be called inside the body of a function component错误:只能在函数组件的主体内部调用挂钩
【发布时间】:2018-12-06 00:02:32
【问题描述】:

大家好,我正在尝试在我的应用程序中使用钩子,但它一直在说 Unhandled Rejection (Invariant Violation):Hooks 只能在函数组件的主体内部调用。我猜我的一个库与钩子冲突,因为我找不到代码有任何问题。

这是我的 package.json,我正在迁移从 recompose 到 hooks 的所有内容...我尝试在测试项目中使用带有 nex.js 的 hooks,它在那里工作正常...

{
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "^7.0.2",
    "next-images": "^1.0.1",
    "react": "^16.7.0-alpha.2",
    "react-dom": "^16.7.0-alpha.2",
    "recompose": "^0.30.0",
    "styled-components": "^4.1.1"
  },
  "devDependencies": {
    "babel-plugin-styled-components": "^1.9.0",
    "eslint": "^5.9.0",
    "eslint-plugin-react": "^7.11.1",
    "eslint-plugin-react-hooks": "^0.0.0"
  }
}

这是我的代码:

import Layout from "../components/Layout";
import Head from "next/head";
import Register from "../blocks/Register";
import InputBox from "../components/InputBox";
import regexUtils from "../utils/regexUtils";
import { useState } from "react";


function BaseComponent() {
    const { errors, onSubmit } = useOnSubmit();
    const [name, setName] = useHandleInputChange("");
    const [email, setEmail] = useHandleInputChange("");
    const [password, setPassword] = useHandleInputChange("");
    const [passwordAgain, setPasswordAgain] = useHandleInputChange("");

    return (
        <Layout>
            <Head>
                <title>Sheet Builder - Register your account</title>
            </Head>
            <Register>
                <Register.Center>
                    <form>
                        <Register.Box>
                            <Register.Title>Create Account</Register.Title>
                            <InputBox error={errors.name} value={name} onChange={setName}>Your Name</InputBox>
                            <InputBox error={errors.email} value={email} onChange={setEmail}>Email</InputBox>
                            <InputBox error={errors.password} value={password} onChange={setPassword} mask>Password</InputBox>
                            <InputBox error={errors.passwordAgain} value={passwordAgain} onChange={setPasswordAgain} mask hint="Passwords must consist of at least 8 characters.">Password Again</InputBox>
                            <Register.Button onClick={onSubmit} type="submit" value="Register your Sheet Builder account" />
                        </Register.Box>
                    </form>
                </Register.Center>
            </Register>
        </Layout>
    );
}

function useHandleInputChange(initialState) {
    const [value, setValue] = useState(initialState);

    const handleInputChange = (e) => {
        setValue(e.target.value);
    };
    return [value, handleInputChange];
}

function useOnSubmit() {
    const initialState =
    {
        email: false,
        name: false,
        password: false,
        passwordAgain: false
    };

    const [errors, setErrors] = useState(initialState);

    const onSubmit = (e)=>{
        e.preventDefault();
        let result = {};
        result.email = !regexUtils.isEmailFormatValid(errors.email);
        result.name = !regexUtils.isFormatValid(errors.name);
        result.password = !regexUtils.isPasswordFormatValid(errors.password);
        result.passwordAgain = !regexUtils.isPasswordFormatValid(errors.passwordAgain);
        setErrors(result);
    };

    return {
        errors, 
        onSubmit
    };
}


export default BaseComponent;

【问题讨论】:

  • 是什么让一个简单的函数成为一个 React 组件?我猜是返回 JSX。你的useHandleInputChange 没有。
  • 你是不是忘了添加import React, {useState} from 'react' 才能将你的函数作为功能组件运行?

标签: reactjs react-hooks


【解决方案1】:

Microsoft Visual Studio 代码调试器似乎存在错误。我从命令行运行程序,之后它运行良好。

【讨论】:

    【解决方案2】:

    你需要导入 react 以便函数可以被视为一个 react 功能组件

    import React, {useState} from 'react';
    

    阅读这篇博文以了解 Why import React from “react” in a functional component?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2021-04-19
      相关资源
      最近更新 更多