Skip to content

Nextjs + Preact + Webpack 5 | Error: Hook can only be invoked from render methods #25

Closed
@tbgse

Description

@tbgse

Hi Preact team. First of all thanks for the amazing work 🌮

Describe the bug
Whenever using any hooks with webpack 5 / next / preact I'm observing the following error when running the dev server:

Error: Hook can only be invoked from render methods.
    at Object.preact__WEBPACK_IMPORTED_MODULE_0__.options.__h (/home/tbgse/dev/webpack-5-test/.next/server/pages/_document.js:1281:7106)

and this error when trying to build

Error occurred prerendering page "/". Read more: https://err.sh/next.js/prerender-error
TypeError: Cannot read property '__H' of undefined
    at hooks_m (/home/tbgse/dev/webpack-5-test/.next/server/pages/_app.js:46:208)
    at hooks_y (/home/tbgse/dev/webpack-5-test/.next/server/pages/_app.js:46:580)

I'm not sure if this is a problem with preact itself or possibly with the next-plugin-preact. This bug does only occur after making the switch to webpack5. Before everything worked just fine. This is not an issue of misusing the hook outside of a render method, the same code is working just fine when using react or preact + webpack4.

To Reproduce
I have created an example repo here:
https://github.com/tbgse/preact-webpack5-next

This includes the following steps

  • Fresh nextjs project on latest version
  • customized next.config.js to enable webpack 5
  • add next-plugin-preact for preact support
  • added a small hello world useEffect hook in pages/index.js

Expected behavior
Hooks should work correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions