question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property '__H' of undefined when I try to use mjs-version of the "hooks" package

See original GitHub issue

Reproduction

Try to import hooks using from the mjs (with no bundlers) as described in the docs: https://preactjs.com/guide/v8/getting-started/ https://preactjs.com/guide/v10/getting-started/#alternatives-to-jsx The Preact itself is being imported fine, but I do not understand how should I import the hooks package properly.

Steps to reproduce

Try to import hooks using mjs modules format like this

<script type="module">
import {useState} from 'https://unpkg.com/preact@10.4.7/hooks/dist/hooks.mjs?module'

export const useFoo = () => {
  const [state, setState] = useState(0);
  return {state, setState};
}
</script>

Expected Behavior

Hooks module is imported successfully

Actual Behavior

I see

Cannot read property '__H' of undefined

error in the console and everything crashes (blank page).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
pokatomnikcommented, Aug 1, 2022

For future searchers, here is a simple snippet for importing the Preact as the native Ecmascript module:

  • The Skypack CDN is used as the only one which has the ES modules CDN I found (yes, the unpkg has this feature as well, but It is still experimental)
  • The HTM is also included as a good replacement for the JSX.
import * as _Preact from 'https://cdn.skypack.dev/preact@10.4.7';
import * as _Hooks from 'https://cdn.skypack.dev/preact@10.4.7/hooks';
import htm from 'https://cdn.skypack.dev/htm@3.0.4';
export const html = htm.bind(_Preact.h);
export default _Preact;
export const Hooks = _Hooks;

I developed a simple template (or an example, as you wish). It’s an explanation how to set up skypack CDN’s preact to be used with the Typescript.

3reactions
vyushincommented, Mar 21, 2021

The same error. I think this is because hooks.mjs imports latest version of preact for every version. Make an issue #3074 image

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot read properties of undefined (reading '__h') - You.com
This works but I'm trying to get hooks to work and I'm running into the issue below. export async function asyncComponentToSync (v: JSX ......
Read more >
React hooks Cannot read properties of undefined using react
Any help is greatly appreciated . Uncaught TypeError: Cannot read properties of undefined (reading 'name'). const SignupSchema = yup.object ...
Read more >
[accept Hook] - Cannot read property 'toBytesSink' of undefined
Hi, I am learning about Hooks and I wanted to try first with something simple and the "accept" hook. I am using your...
Read more >
Cannot read properties of undefined' - JavaScript Debugging
Join Discord https://selftaught-dev.com/join-discord//r/SelfTaughtDev - https://www.reddit.com/r/selftaughtdevGet My Resume Template Free ...
Read more >
A Deno Module for Handling Null and Undefined Values
<script type="module"> import { read, writeFileXLSX } from "https://cdn.sheetjs.com/xlsx-0.18.6/package/xlsx.mjs"; /* load the codepage support library for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found