Preact: 10.0.0-alpha.2 - Hooks - Uncaught TypeError: Cannot read property 'render' of undefined

Created on 19 Mar 2019  路  10Comments  路  Source: preactjs/preact

Using hooks like this.

import {useState} from 'preact/hooks';

getting this error

hooks.mjs:1 
Uncaught TypeError: Cannot read property 'render' of undefined
    at Module../node_modules/preact/hooks/dist/hooks.mjs (hooks.mjs:1)
    at __webpack_require__ (bootstrap:63)
    at Module../src/index.js (index.js:2)
    at __webpack_require__ (bootstrap:63)
    at Object.2 (rx-preact-app.bundle.js:20462)
    at __webpack_require__ (bootstrap:63)
    at bootstrap:195
    at bootstrap:195
bug needs-more-info

All 10 comments

also i've got

import {h, render} from 'preact'; 

interesting, what are you using to bundle your app?

Can't reproduce it with the following snippet: https://codesandbox.io/s/ly2k4pwqnm

I'm bundling with webpack

.babelrc

{
  "presets": [
    [
      "@babel/env",
      {
        "targets": {
          "browsers": ["last 2 versions", "safari >= 7"]
        }
      }
    ]
  ],
  "plugins": [
    [
      "@babel/plugin-transform-react-jsx",
      {
        "pragma": "h"
      }
    ],
    "@babel/plugin-syntax-dynamic-import"
  ]
}

@marvinhagemeister I'm reviewing my set up with webpack.

It could also be related to whacky module resolution of .mjs files in webpack. If that's the case this can be fixed via the webpack-modules plugin. We're evaluating switching to .js instead of .mjs but for now this workaround should do the job :+1:

@marvinhagemeister that fixed it!! I hope we go back to regular .js
I'm not sure about the usefulness of mjs for front-end apps. Is there an issue I can track when you'll make the change? or just keep a look out at the next release.

@didymu5 yeah with Myles finally fixing the module mess in node it seems like the mjs situation will be settled not too far in the future. We somewhat track it in #1321 although the issue title admittedly doesn't really make that obvious.

EDIT: There you go #1425 :tada:

Can you release a new alpha release of preact X? Right now I have to set up a resolve alias in my webpack config to be even able to build my application when I import hooks into it. This is a quite major issue, as it breaks correct code.

That's the plan 馃帀 Already prepared the release notes yesterday and the next alpha should go out in ~15 h.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jescalan picture jescalan  路  3Comments

marcosguti picture marcosguti  路  3Comments

matthewmueller picture matthewmueller  路  3Comments

skaraman picture skaraman  路  3Comments

mizchi picture mizchi  路  3Comments