Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the react-hook-form in class component #22

Closed
sboopal opened this issue Apr 30, 2019 · 10 comments
Closed

Using the react-hook-form in class component #22

sboopal opened this issue Apr 30, 2019 · 10 comments

Comments

@sboopal
Copy link

sboopal commented Apr 30, 2019

How do i add the hook into a class component? the current examples show only to add it to function component?

Getting this error when I try to add it in a function

Uncaught Invariant Violation: 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
    See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
    at invariant (http://localhost:3000/bundle.js:45863:15)
    at Object.throwInvalidHookError (http://localhost:3000/bundle.js:58675:3)
    at useRef (http://localhost:3000/bundle.js:70887:21)
    at useForm (http://localhost:3000/bundle.js:67453:75)
    at BannerReport.render (http://localhost:3000/bundle.js:82491:85)
    at finishClassComponent (http://localhost:3000/bundle.js:60549:31)
    at updateClassComponent (http://localhost:3000/bundle.js:60504:24)
    at beginWork (http://localhost:3000/bundle.js:61452:16)
    at performUnitOfWork (http://localhost:3000/bundle.js:65120:12)
    at workLoop (http://localhost:3000/bundle.js:65160:24)
@bluebill1049
Copy link
Member

bluebill1049 commented Apr 30, 2019

Hi @sboopal ,

Reason for you to getting the error.

React Hook Form using React Hook which required to have version 16.8.

  "peerDependencies": {
    "react": "^16.8.0",
    "react-dom": "^16.8.0"
  }

The benefit of React Hook is to let you use state and other React features without writing a class, but at the same time it won't work with Class component, hence React Hook Form will not work with Class component.

From React documentation:

You can’t use Hooks inside of a class component, but you can definitely mix classes and function components with Hooks in a single tree. Whether a component is a class or a function that uses Hooks is an implementation detail of that component. In the longer term, we expect Hooks to be the primary way people write React components.

you can refer examples to get a quick start: https://github.com/bluebill1049/react-hook-form/tree/master/examples

@bluebill1049
Copy link
Member

hey @sboopal , how did you go with functional component instead of class component? let me know if you need some help.

@sboopal
Copy link
Author

sboopal commented May 2, 2019 via email

@bluebill1049
Copy link
Member

here is a quick sandbox for validating couple selections. (functional component)

https://codesandbox.io/s/qk47j6854j

hope you find this useful

cheers
bill

@sboopal
Copy link
Author

sboopal commented May 4, 2019 via email

@bluebill1049
Copy link
Member

bluebill1049 commented May 4, 2019

Firstly, make sure you check your package.json for React version, is it 16.8 above?

If so, then it is better to convert to functional component and try out react-hook-form with the example I have supplied above.

If you want more examples, which can be found here:
https://github.com/bluebill1049/react-hook-form/tree/master/examples

API has been fully documented here too:
https://react-hook-form.now.sh/api

let me know if these are helpful.

cheers
bill

@bluebill1049
Copy link
Member

Hey @sboopal, is there anything I can help more? otherwise, I may have to close this issue.

@sboopal
Copy link
Author

sboopal commented May 7, 2019 via email

@bluebill1049
Copy link
Member

hope you get the chance to use react hook form in the future :)

@samantaavijit
Copy link

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants