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.

Preact useEffect issues

See original GitHub issue

Hi, 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:closed
  • Created a year ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
imaginamundocommented, Jul 6, 2022

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

1reaction
iamsahilsonawanecommented, Jul 8, 2022

@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:

export default function AnyIslandComponent() {
  log.warning("outside");

  useEffect(() => {
    log.warning("EFFECT");
  });

  return <div>Logging issue</div>;
}

Results:

Terminal console:

outside

Browser dev tools console:

outside EFFECT


Dev Tools recording snippet

https://user-images.githubusercontent.com/47249618/177977056-0cd10eec-e32e-444a-b64f-2cb91219f197.mp4

Read more comments on GitHub >

github_iconTop 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 >

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