fresh icon indicating copy to clipboard operation
fresh copied to clipboard

Preact useEffect issues

Open Rafie97 opened this issue 4 years ago • 11 comments

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.

Rafie97 avatar Jul 06 '22 00:07 Rafie97

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

imaginamundo avatar Jul 06 '22 01:07 imaginamundo

@imaginamundo Thanks for the reply, unfortunately that's not working either. I tried putting it into the Counter island that comes by default and nothing logs, once again with all the variations

Rafie97 avatar Jul 07 '22 05:07 Rafie97

@Rafie97, that's strange. Could you please provide your folder structure and the code you've tried to run?

nikableh avatar Jul 07 '22 09:07 nikableh

@synalice Hey, sorry this is my other account. It's just as described in the original comment. Created a fresh app and put

  log.warning("outside"); // Works

  useEffect(() => {
    log.warning("EFFECT"); // Doesn't work
  });

Into the default Counter island that comes boilerplate. The "outside" logs just fine in the console, but the useEffect just never runs. It knows what it is from preact, just does not happen with any dependency array

rafa-icon avatar Jul 07 '22 18:07 rafa-icon

Did you try an empty array for the dependency?

useEffect(() => {
    log.warning("EFFECT"); // Doesn't work
  } [] );

Remember this will only run once on mount.

nhrones avatar Jul 07 '22 19:07 nhrones

@nhrones, doesn't the useEffect() function run every time the component updates?

nikableh avatar Jul 07 '22 20:07 nikableh

@nhrones Yes, I have tried with an empty dependency array and with the count state that comes boilerplate as well.

Rafie97 avatar Jul 08 '22 01:07 Rafie97

@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

iamsahilsonawane avatar Jul 08 '22 10:07 iamsahilsonawane

@synalice The useEffect hook in preact, when called with no dependencies, is the same as a React class, componentDidMount. That means that it only fires once on mount, not on every refresh!
Also, if used in a Fresh island, then yes it only runs in the browser. That means it should only show up in the browser console.

nhrones avatar Jul 08 '22 14:07 nhrones

Yes with @nhrones's comment I get it now. As the island is rendered on the client-side (and though the terminal console basically acts from the server-side) is the reason we don't see the logs in the terminal but in the browser console.

So well, this is expected

iamsahilsonawane avatar Jul 08 '22 14:07 iamsahilsonawane

Ah I see, can't believe I didn't check the browser console for the island case. Works for me

Rafie97 avatar Jul 09 '22 00:07 Rafie97