Preact useEffect issues
See original GitHub issueHi, I’ve made a brand new fresh project with deno run -A -r https://fresh.deno.dev my-app
, but cannot get useEffect/useLayoutEffect to log a message:
import { h } from "preact";
import { useEffect, useState } from "preact/hooks";
import * as log from "log";
import { tw } from "@twind";
import Counter from "../islands/Counter.tsx";
export default function Home() {
log.warning("outside"); // Works
useEffect(() => {
log.warning("EFFECT"); // Doesn't work
});
return (
<div class={tw`p-4 mx-auto max-w-screen-md`}>
<p class={tw`my-6`}>
Welcome to `fresh`. Try update this message in the ./routes/index.tsx
file, and refresh.
</p>
<Counter start={3} />
</div>
);
}
Please tell me what I’m doing wrong, I’ve tried many combinations of useEffect’s dependency array and triggering useStates. I’ve also tried adding and removing preact/hooks in the import_map.json.
Issue Analytics
- State:
- Created a year ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Hooks | Preact: Fast 3kb React alternative with the same ES6 ...
The first argument to useEffect is an argument-less callback that triggers the effect. In our case we only want to trigger it, when...
Read more >10.0.0-alpha.0 - Hook useEffect never gets called #1328
When using a code like this in 10.0.0-alpha.0: import { useEffect } from 'preact/hooks'; function Component(props) { useEffect(() ...
Read more >PReact useEffect doesn't trigger on state change
1 Answer 1 · Nevermind, I did try it and still the useEffect doesn't get triggered, the weird thing is that the imageList...
Read more >Preact — Side Effect Hooks - JavaScript in Plain English
In this article, we'll look at how to get started with front end development with Preact. useEffect. We use the useEffect hook to...
Read more >React.useEffect Hook – Common Problems and How to Fix ...
The infinite re-renders problem ... The reason our component is re-rendering is because our useEffect dependency is constantly changing. But why?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hii, useEffect is a client side hook to preact. It means that it runs in the browser. Fresh uses the islands based client hydration, to you have a javascript generated to the client, you have to create a component inside the folder islands and then import it. After this useEffect should work fine.
Read more about it here: https://fresh.deno.dev/docs/concepts/islands
@rafa-icon @Rafie97 I can replicate this issue. For now, you can use the browser dev tools console to see the logs.
Issue Statement
@lucacasonato The logs outside the preact elements log the statement in the console. But logs written in the preact components (like
useEffect
) are not rendered in the terminal console but in the dev tools console.Reproducible code
Referencing the same code as above:
Results:
Terminal console:
Browser dev tools console:
Dev Tools recording snippet
https://user-images.githubusercontent.com/47249618/177977056-0cd10eec-e32e-444a-b64f-2cb91219f197.mp4