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.

10.0.0-alpha.1 - __hooks of undefined with render-to-string

See original GitHub issue

There’s an odd bug when using hooks together with render-to-string.

What I had was a simple component with the useState hook. Rendered on the client this was fine, on the server it resulted in: Uncaught TypeError: Cannot read property '__H' of undefined.

At first I thought this had something to do with render-to-string because the error also occurred when I replaced preact’s render function on the client with render-to-string. After some more investigation this wasn’t actually a bug in that library but rather in preact/hooks, let me explain:

The __H in the error stands for __hooks that can’t be read because currentComponent is undefined. This is defined when you use preact’s render function. In this codesandbox you can see that at first render-to-string throws the error, after that the same component renders fine (normal render function), then render-to-string works!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

16reactions
mcclurecommented, Dec 10, 2019

Leaving this for the next person who makes the mistake I made:

If you are new to Preact and you found this bug by searching “Cannot read property ‘__H’ of undefined” on Google: That error message can also happen if you call useState and you are not actually in a component function. I recommend enabling “preact/debug”: https://preactjs.com/guide/v10/debugging/

8reactions
developitcommented, Dec 10, 2019

Thanks for pointing folks in the right direction @mcclure! I wonder if it would be worth creating a page on preactjs.com that lists out common error messages with fixes for each? That way anyone googling gets a nice resource with a clear answer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot read properties of undefined (reading '__h') - You.com
Below I'm trying to create a "async render" that will resolve the preact component tree down to one promise then I can resolve...
Read more >
currentComponent is undefined, __hooks not available
import { render as renderToString } from "preact-render-to-string";. import { useState } from "preact/hooks";. function App() {.
Read more >
ReactDOMServer – React
renderToString ()​​ Render a React element to its initial HTML. React will return an HTML string. You can use this method to generate...
Read more >
Is it ok to use ReactDOMServer.renderToString in the browser ...
renderToString are useful for React's internal DOM manipulation when re-rendering. Unless I'm mistaken, the best one to use probably depends on ...
Read more >
What is the renderToString() method in React? - Educative.io
Explanation · Line 1: First, we need to import or require the React . · Line 2: Import react-dom/server module in our server...
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