preactjs / preact

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
https://preactjs.com
MIT License
36.35k stars 1.93k forks source link

useEffect never called #4417

Closed trusktr closed 2 weeks ago

trusktr commented 2 weeks ago

Describe the bug

I have this code:

/** @jsx h */
import { h, render } from 'preact';
import { useEffect } from 'preact/hooks';

function Container(props) {
  return <div style="width: 100%; height: 100%;">{props.children}</div>;
}

const App = () => {
  console.log('App');

  useEffect(() => {
    console.log('EFFECT !!!!!!!!!!!');
  }, []);

  return <Container></Container>;
};

render(<App />, document.body);

The hook is never called, the EFFECT log never happens.

Expected behavior

Isn't the effect supposed to run once?

Also the DOM content in the <body> is just <undefined>.

To Reproduce

https://stackblitz.com/edit/preact-starter-template-jzkt7e?file=App.jsx

(Heads up, this is a fork of the stackblitz that is the number one result for "stackblitz preact" on Google)

trusktr commented 2 weeks ago

Don't know why that one doesn't work, but this one works:

https://stackblitz.com/edit/create-preact-starter-kak5bf?file=src%2Findex.jsx

(this one is forked from a result I found lower in the search results.)

rschristian commented 2 weeks ago

Looks like a fairly buggered up example, with the empty HTML document and all.

StackBlitz has a Preact app on the front page under the 'FrontEnd' tab, we have an additional StackBlitz template for our issues here, and when in doubt, try our REPL.

Closing, as it's not an issue.