"TypeError: Cannot read properties of null (reading 'control')" when using useWatch
See original GitHub issueDuplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
There is an example in the storybook page for the useWatch method on how to listen to form field changes (stories/FormContainer.stories.tsx
). When I try using it in the nextjs example. I am getting an Error:
TypeError: Cannot read properties of null (reading 'control')
at useWatch (file:///Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-hook-form/dist/index.esm.mjs:296:31)
at SubComponent (webpack-internal:///./src/pages/index.tsx:24:84)
at renderWithHooks (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5471:16)
at renderIndeterminateComponent (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5544:15)
at renderElement (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5759:7)
at renderNodeDestructive (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5898:11)
at renderNode (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6030:12)
at renderChildrenArray (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5986:7)
at renderNodeDestructive (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5918:7)
at renderNode (/Users/chris/Work/github/other/react-hook-form-mui/example/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6030:12)
error - TypeError: Cannot read properties of null (reading 'control')
This error only appears when refreshing the page. If there is a bit of a delay at the page load, the form works just fine. So my workaround for now is adding a timeout of one second at page load before rendering the form. I have no idea what causes the issue since using the watcher from react-hook-form directly is working just fine for me.
Expected behavior 🤔
No Error when refreshing the page
Steps to reproduce 🕹
Steps:
- clone the react-hook-form-mui repo
- go to the example folder
- add the SubComponent from
stories/FormContainer.stories.tsx
to the form insrc/pages/index.tsx
and use it in the form. change the useWatch to the fields that are present in the form or add the extra fields as well - the hot reload is working but try refreshing the page
Issue Analytics
- State:
- Created a year ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
TypeError: Cannot read property 'control' of null #5150 - GitHub
TypeError: Cannot read property 'control' of null. ... this means your input is not wrapped with FormProvider. View full answer ...
Read more >useWatch | React Hook Form - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >React Hooks : TypeError: Cannot read property 'firstName' of null
The error makes sense, you are using Object notation to reach the value belonging to something that is not an object. Just set...
Read more >typeerror: cannot read properties of null (reading 'usecontext')
This error is usually due to using a React Context API without a Provider component. The Provider component is responsible for passing context...
Read more >Change Log - Ant Design
Fix FloatButton tooltip property is not support 0 value. · Fix Space wrapped Select not display clear icon problem when mouse hover. ·...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
awesome, thank you!!
@slaat I finally had the time to restructure the repo and now I have a better testing environment for both Storybook and NextJS. The new version
5.6.0
re-exports the API ofreact-hook-form
. Now you can use theuseWatch
hook fromreact-hook-form-mui
and the context issue is resolved. https://github.com/dohomi/react-hook-form-mui/blob/master/apps/nextjs/src/pages/withSub.tsx#L5