简体   繁体   English

挂钩的调用在功能组件内部给出错误挂钩只能在 function 组件的主体内部调用

[英]Call of hooks is inside of functional component give error Hooks can only be called inside of the body of a function component

I have component language for translations我有用于翻译的组件语言

const Language = (props: IOwnProps) => {
    // error is in next line for useSelector 
    const language = useSelector(
        (state: IState) => state.currentLang
    );
    
    return getTranslations(
        props.languageString
    );
};

in form I have validation using formik在表单中我使用 formik 进行验证

const validationSchema = () => {
    const requiredFirstName = Language({
        languageString: firstNameRequired,
    });
    return yup.object({
        firstName: yup
            .string()
            .required(requiredFirstName)
    });
};

here is form component这是表单组件

const UserForm = ({
    userData: userData
    setErrorIndex,
}: UserFormProps) => {
    const formik = useFormik({
        initialValues: {
            userData: userData.firstName,
        },
        validationSchema,
        onSubmit: (values) => {
            const playerDataLocal = {
                firstName: values.firstName,
            };
            handleSubmit(playerDataLocal);
        },
    });

    return (
        <form onSubmit={formik.handleSubmit}>
            <TextField
                id="firstName"
                name="firstName"
                label="First Name *"
                defaultValue={formik.values.firstName}
                onChange={formik.handleChange}
                error={formik.touched.firstName && Boolean(formik.errors.firstName)}
                helperText={formik.touched.firstName && formik.errors.firstName}
                fullWidth
            />
        </form>
    );
};
export default UserForm;

in validation schema line with Language give an error: Invalid hook call.在语言验证模式行中给出错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。

Call of Language is from validationSchema that is a functional component call stack of error is in line of useSelector from Language call of validationSchema is inside of 'useFormik' can be this the issue? Language调用来自validationSchema ,它是一个功能组件调用堆栈错误符合useSelector来自validationSchemaLanguage调用在“useFormik”内部可能是这个问题吗? any ideas?有任何想法吗?

It's because validationSchema is not a React component.这是因为validationSchema不是 React 组件。 You can only use hooks inside of a functional component and since validationSchema returns something other than JSX or another component it is not classed as a React component.您只能在功能组件内部使用钩子,并且由于validationSchema返回的不是 JSX 或其他组件,因此它不被归类为 React 组件。

You will probably want to move your hook call up inside UserForm and then pass the result into validationSchema as an argument.您可能希望将您的挂钩调用移动到UserForm中,然后将结果作为参数传递给validationSchema

暂无
暂无

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

相关问题 收到此错误“无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。” 在反应功能组件内? - Getting this error “Invalid hook call. Hooks can only be called inside of the body of a function component.” inside a react functional component? 钩子只能在函数组件错误的主体内部调用 - Hooks can only be called inside the body of a function component error 错误。 错误:无效挂钩调用。 钩子只能在 function 组件的内部调用 - Error. Error: Invalid hook call. Hooks can only be called inside of the body of a function component 我已经在使用功能组件,但我不断收到错误消息:只能在功能组件的主体内调用钩子 - I'm already using a functional component and I keep getting error : Hooks can only be called inside the body of a function component 我不断收到:错误:无效的挂钩调用。 Hooks 只能在函数组件内部调用 - I keep getting:Error: Invalid hook call. Hooks can only be called inside of the body of a function component 未捕获的错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用吗? - Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component? 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component 挂钩调用错误:只能在 function 组件的主体内部调用挂钩 - hook call error: Hooks can only be called inside of the body of a function component React 17:错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React 17: Error: Invalid hook call. Hooks can only be called inside of the body of a function component React Native:错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native: Error: Invalid hook call. Hooks can only be called inside of the body of a function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM