Error: Hook can only be invoked from render methods.
See original GitHub issueDescribe the bug
I upgraded to nextjs v12 and got this error:
Error: Hook can only be invoked from render methods.
The error gets thrown on every other react hook as well.
To Reproduce
store.js
import { createContext } from "preact";
import { useReducer, useContext } from "preact/hooks";
const reducer = (state, action) => {
switch (action.type) {
case "filter":
return { ...state, filter: action.data };
default:
return;
}
};
const initialState = {
filter: { category: null },
};
const StoreContext = createContext(initialState);
export function StoreProvider(props) {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<StoreContext.Provider value={{ state, dispatch }}>
{props.children}
</StoreContext.Provider>
);
}
export function useStore() {
const store = useContext(StoreContext);
return store;
}
Steps to reproduce the behavior:
- Add store.js to a nextjs v12 project
- See error
Expected behavior The code should just work as before.
Additional Info package.json
{
"private": true,
"license": "The Unlicense",
"scripts": {
"dev": "npx netlify-cms-proxy-server & next dev",
"build": "next build",
"start": "next start",
"postbuild": "next-sitemap"
},
"dependencies": {
"framer-motion": "^5.2.1",
"next": "^12.0.3",
"next-mdx-remote": "^3.0.7",
"next-plugin-preact": "^3.0.6",
"next-pwa": "^5.4.0",
"preact": "^10.5.14",
"preact-render-to-string": "^5.1.19",
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-intersection-observer": "^8.32.2",
"react-lazy-hydration": "^0.1.0",
"react-responsive": "^9.0.0-beta.4",
"react-ssr-prepass": "npm:preact-ssr-prepass"
},
"devDependencies": {
"@tailwindcss/forms": "^0.4.0-alpha.1",
"@tailwindcss/typography": "^0.5.0-alpha.2",
"autoprefixer": "^10.4.0",
"eslint": "<8.0.0",
"eslint-config-next": "12.0.3",
"gray-matter": "^4.0.3",
"netlify-cms-proxy-server": "^1.3.22",
"next-sitemap": "^1.6.203",
"postcss": "^8.3.11",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-preset-env": "^6.7.0",
"sharp": "^0.29.2",
"tailwindcss": "^3.0.0-alpha.1"
}
node --version
v16.11.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:22 (7 by maintainers)
Top Results From Across the Web
React Hooks Error: Hooks can only be called inside the body ...
"hotReplacementRender". RHL is trying to do React's job, and render the old and the new app, to merge them. So, obviously, that's broken...
Read more >Invalid Hook Call Warning - React
You can only call Hooks while React is rendering a function component: ✓ Call them at the top level in the body of...
Read more >Invalid hook call error - Shakacode
Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1....
Read more >A Guide to Common React Errors
Any of these are valid as return values for React render methods ... Error: Hooks can only be called inside the body of...
Read more >"Hooks can only be called inside the body of a ... - Owen Conti
Most common: You're breaking the rules of hooks. You must only call hooks while React is rendering a function component. #The rules of...
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
happens to me as well while using
react-hook-form
andNext js
^v12.xThanks for the clarification @scheatkode. I installed the
react@npm:@preact/compat
andreact-dom@npm:@preact/compat
packages but the issue remained for me.In the end, I got it working by installing the latest versions of
preact
andpreact-cli
. I must have fallen a bit behind regarding the latest releases.Thanks for your quick reply