-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Error: Hook can only be invoked from render methods. #3308
Comments
Hey, could you give an update please about what the problem is and if you already had time to work on it? |
Well, I've been debugging a similar issue on Next. Adding |
Thank you very much. Guess I can close this now and follow the nextjs issue. :) |
Oh feel free to keep it open, you can test whether this applies to you as well by adding getServerSideProps |
Hey, just upgraded to next 12 again. Seems like there is a problem with the compat exports now: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './compat/package.json' is not defined by "exports" in /home/user/Projects/bestnotebook-tech/node_modules/preact/package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)
at packageExportsResolve (node:internal/modules/esm/resolve:692:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.mod._resolveFilename (/home/user/Projects/bestnotebook-tech/node_modules/next/dist/build/webpack/require-hook.js:171:28)
at Function.Module._resolveFilename (/home/user/Projects/bestnotebook-tech/node_modules/module-alias/index.js:49:29)
at Function.resolve (node:internal/modules/cjs/helpers:108:19)
at getPackagePath (/home/user/Projects/bestnotebook-tech/node_modules/next/dist/build/webpack-config.js:543:41) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
error Command failed with exit code 1. 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.3.3",
"next": "^12.0.4",
"next-mdx-remote": "^3.0.7",
"next-plugin-preact": "^3.0.6",
"next-pwa": "^5.4.1",
"preact": "^10.6.1",
"preact-render-to-string": "^5.1.19",
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-intersection-observer": "^8.32.5",
"react-lazy-hydration": "^0.1.0",
"react-responsive": "^9.0.0-beta.5",
"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.3.0",
"eslint-config-next": "12.0.4",
"gray-matter": "^4.0.3",
"netlify-cms-proxy-server": "^1.3.22",
"next-sitemap": "^1.6.203",
"postcss": "^8.4.3",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-preset-env": "^7.0.1",
"sharp": "^0.29.2",
"tailwindcss": "^3.0.0-alpha.1"
}
} |
Will be in the next release |
@tw1t611 this has been published in 10.6.2 |
I'll close this for now, feel free to comment in case there are more issues |
Hey, came up again. :( 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.3.3",
"next": "^12.0.5",
"next-mdx-remote": "^3.0.7",
"next-plugin-preact": "^3.0.6",
"next-pwa": "^5.4.1",
"preact": "^10.6.2",
"preact-render-to-string": "^5.1.19",
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-intersection-observer": "^8.32.5",
"react-lazy-hydration": "^0.1.0",
"react-responsive": "^9.0.0-beta.5",
"react-ssr-prepass": "npm:preact-ssr-prepass",
"scheduler": "^0.20.2"
},
"devDependencies": {
"@tailwindcss/forms": "^0.4.0-alpha.1",
"@tailwindcss/typography": "^0.5.0-alpha.2",
"autoprefixer": "^10.4.0",
"eslint": "8.4.1",
"eslint-config-next": "12.0.7",
"gray-matter": "^4.0.3",
"netlify-cms-proxy-server": "^1.3.22",
"next-sitemap": "^1.6.203",
"postcss": "^8.4.3",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-preset-env": "^7.0.1",
"sharp": "^0.29.2",
"tailwindcss": "^3.0.0-alpha.1"
}
} |
@JoviDeCroock Hey, I am a little late, but I checked the issue again and it still isn't working for me. Running
After installing the Do you need more info for reproduction? {
"private": true,
"license": "The Unlicense",
"scripts": {
"dev": "npx netlify-cms-proxy-server & next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"postbuild": "next-sitemap"
},
"dependencies": {
"amazon-product-api": "^0.4.4",
"framer-motion": "^5.5.6",
"next": "^12.0.7",
"next-mdx-remote": "^3.0.7",
"next-plugin-preact": "^3.0.6",
"next-pwa": "^5.4.1",
"preact": "^10.6.4",
"preact-render-to-string": "^5.1.19",
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-intersection-observer": "^8.32.5",
"react-lazy-hydration": "^0.1.0",
"react-responsive": "^9.0.0-beta.5",
"react-ssr-prepass": "npm:preact-ssr-prepass",
"tailwindcss-elevation": "^1.0.1"
},
"devDependencies": {
"@tailwindcss/forms": "^0.4.0-alpha.1",
"@tailwindcss/typography": "^0.5.0-alpha.2",
"autoprefixer": "^10.4.2",
"eslint": "^8.6.0",
"eslint-config-next": "12.0.7",
"gray-matter": "^4.0.3",
"netlify-cms-proxy-server": "^1.3.23",
"next-sitemap": "^1.6.245",
"postcss": "^8.4.5",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-preset-env": "^7.2.0",
"sharp": "^0.29.2",
"tailwindcss": "^3.0.12"
}
} |
got the same error when adding unistore to a preact project |
happens to me as well while using |
Happens to me as well. Here is minimal repro (using // package.js
"dependencies": {
"next": "12.0.8",
"next-plugin-preact": "^3.0.6",
"preact": "^10.6.4",
"preact-render-to-string": "^5.1.19",
"react-ssr-prepass": "npm:preact-ssr-prepass@^1.2.0",
"react": "npm:@preact/compat@^17.0.3",
"react-dom": "npm:@preact/compat@^17.0.3",
"react-redux": "^7.2.6",
"redux": "^4.1.2"
},
"devDependencies": {
"@types/node": "17.0.8",
"@types/react": "17.0.38",
"eslint": "8.6.0",
"eslint-config-next": "12.0.8",
"scheduler": "^0.20.2",
"typescript": "4.5.4"
} // src/store.js
import { createStore, combineReducers } from 'redux';
const appReducer = combineReducers({});
const reducer = (state, action) => {
return appReducer(state, action);
};
const store = createStore(reducer, {});
export default store; // src/pages/_app.js
import { h } from 'preact';
import { Provider } from 'react-redux';
import store from '../store';
const MyApp = ({ Component, pageProps }) => {
const getLayout = Component.getLayout || ((page) => page);
return (
<Provider store={ store }>
{ getLayout(<Component { ...pageProps } />) }
</Provider>
);
};
export default MyApp; // src/pages/index.js
import { h } from 'preact';
import { useState } from 'preact/hooks';
const Login = () => {
const [ counter ] = useState(1);
return (
<div>hello {couter}</div>
);
};
const LoginLayout = ({ children }) => <article>{ children }</article>
export default Login;
Login.getLayout = function getLayout(page) {
return (
<LoginLayout>
{ page }
</LoginLayout>
);
}; |
FWIW I bumped down next to v11.1.3 and error is gone |
As a workaround for anyone bumping into this issue, try importing from import { useReducer, useState } from 'react' Hoping this helps. |
Has anyone found a clear fix for this issue? I tried @scheatkode solution but the preact/debug package stills shows the I'm trying to build and deploy my Preact app via Docker with Nginx.
And my useReducer hook
|
Hey @acolle, the workaround works because of Hoping this helps |
Thanks for the clarification @scheatkode. In the end, I got it working by installing the latest versions of Thanks for your quick reply |
Did you find a solution? |
maybe this is a general workaround? example error: solved: nano node_modules/.pnpm/@storybook+ui@6.5.9_dmmjpkrptnkrz2kzqikv5vt4mi/node_modules/react/package.json just add {
"public": true,
"name": "@preact/compat",
"version": "17.1.1",
"description": "Alias of preact/compat",
"main": "./index.js",
"module": "./index.mjs",
"exports": {
".": {
"module": "./index.mjs",
"import": "./index.mjs",
"require": "./index.js"
},
"./server": {
"module": "./server.mjs",
"import": "./server.mjs",
"require": "./server.js"
},
"./server.browser": {
"module": "./server.mjs",
"import": "./server.mjs",
"require": "./server.js"
},
"./jsx-dev-runtime": {
"module": "./jsx-dev-runtime.mjs",
"import": "./jsx-dev-runtime.mjs",
"require": "./jsx-dev-runtime.js"
},
"./jsx-runtime": {
"module": "./jsx-runtime.mjs",
"import": "./jsx-runtime.mjs",
"require": "./jsx-runtime.js"
},
"./package.json": "./package.json"
},
"repository": "preactjs/compat-alias-package",
"author": "Preact Team <team@preactjs.com>",
"license": "MIT",
"homepage": "https://preactjs.com",
"peerDependencies": {
"preact": "*"
},
"devDependencies": {
"preact": "^10.5.15"
}
} My question: is a good idea add this to the official |
Any news about this? Just created a new next.js project with latest preact and I get this error :( EDIT: Works fine if I add |
using Preact@10.11.2 + Vite@3.2.0 I'm getting this same error when trying to upload multiple images to firebase storage, everything was working fine: I added state to a custom hook for holding return value from getDownloadURL(). Some code for reference:
|
I'm having this problem as described here: preactjs/preact-ssr-prepass#48 // preact/debug/src/debug.mjs line 255
options._hook = (comp, index, type) => {
if (!comp || !hooksAllowed) {
throw new Error('Hook can only be invoked from render methods.');
}
if (oldHook) oldHook(comp, index, type);
}; |
Describe 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
Steps to reproduce the behavior:
Expected behavior
The code should just work as before.
Additional Info
package.json
The text was updated successfully, but these errors were encountered: