javascript - React hook,发生Invalid hook call错误

标签 javascript reactjs react-hooks

我正在使用 React Hooks 构建项目,但在下面出现此错误。

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

这是下面的代码

const authRequest = (e: any) => {
  e.preventDefault();
  alert('Error!')
  const [authRequestState, authRequestTrue] = React.useState(false)
  authRequestTrue(true)
}


const renderFormikForm = () => {
  return (
    <Formik initialValues={{country: '', number: ''}} onSubmit={(values) => {submitForm(values)}}>
      {({ values, errors, touched, handleChange, handleBlur}) => (
        <form>
          <div className='input-box'>
            <p className='input'>
              <input type='email' name='email' placeholder='emial' value='libeto@commontown.co'/>
            </p>
            <p className='input'>
              <input type='number' name='number' placeholder='number' value={values.number} onChange={handleChange} style={{width: '50%'}} />
              <button onClick={(e) => authRequest(e)}><em><a>Click!!!</a></em></button>
            </p>
          </div>
        </form>
      )}
    </Formik>
  )
}

所以基本上,功能组件呈现 renderFormikForm 组件 并且当我单击按钮(比如 Click!!!) onClick 触发 authRequest 函数但状态被更改时,它给我上面提到的错误.

最佳答案

Hooks 只能在函数组件内部创建。您需要在函数组件中使用 useState

将您的代码更新为以下内容:


const renderFormikForm = () => {
  const [authRequestState, authRequestTrue] = React.useState(false)

  const authRequest = (e: any) => {
    e.preventDefault();
    alert('Error!')
    authRequestTrue(true)
  }
  return (
    <Formik initialValues={{country: '', number: ''}} onSubmit={(values) => {submitForm(values)}}>
      {({ values, errors, touched, handleChange, handleBlur}) => (
        <form>
          <div className='input-box'>
            <p className='input'>
              <input type='email' name='email' placeholder='emial' value='libeto@commontown.co'/>
            </p>
            <p className='input'>
              <input type='number' name='number' placeholder='number' value={values.number} onChange={handleChange} style={{width: '50%'}} />
              <button onClick={(e) => authRequest(e)}><em><a>Click!!!</a></em></button>
            </p>
          </div>
        </form>
      )}
    </Formik>
  )
}

或者你也可以重写如下:

const authRequest = (e: any, authRequestTrue) => {
  e.preventDefault();
  alert('Error!')
  authRequestTrue(true)
}
const renderFormikForm = () => {
  const [authRequestState, authRequestTrue] = React.useState(false)

  return (
    <Formik initialValues={{country: '', number: ''}} onSubmit={(values) => {submitForm(values)}}>
      {({ values, errors, touched, handleChange, handleBlur}) => (
        <form>
          <div className='input-box'>
            <p className='input'>
              <input type='email' name='email' placeholder='emial' value='libeto@commontown.co'/>
            </p>
            <p className='input'>
              <input type='number' name='number' placeholder='number' value={values.number} onChange={handleChange} style={{width: '50%'}} />
              <button onClick={(e) => authRequest(e, authRequestTrue)}><em><a>Click!!!</a></em></button>
            </p>
          </div>
        </form>
      )}
    </Formik>
  )
}

后一个更接近问题中提到的代码。

希望对您有所帮助。如有任何疑问,请回复。

关于javascript - React hook,发生Invalid hook call错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390453/

相关文章:

javascript - 在 qt 应用程序中集成 Svg 和 Javascript

javascript - 如何在 React Redux 应用程序中打开和关闭内联对话框

javascript - SSR React应用程序的渲染过程和异步代码执行

reactjs - react useMemo 缺少依赖

javascript - 如何在javascript中更改JSON文件的编码

asp.net - 在网页上动态调整 Silverlight 控件的大小

javascript - 尝试用jQuery让div随机出现,效果不理想

javascript - Reactjs 从 Dexie.js 中删除一个项目

javascript - 与 useEffect 一起使用时,自定义钩子(Hook)会被无限调用

reactjs - react useCallback : useCallback is still re-rendering function each time