2

I am trying to build a web app with Astro + Reactjs, but I got an issue on calling useEffect. Basically useEffect is not calling, I don't get any logs in the terminal or any warnings/errors in the terminal or in the browser.

I am exporting the function as: export default function SecondSection(){}, I changed the file extension from .jsx to .tsx, still no result. I followed all instructions from astro docs to integrate react.

I am trying to use react hooks, like useEffect/useState, but for some reasons it's not working any of that.

What can cause that issue? Thank you for your time. enter image description here

cucereanum
  • 43
  • 8

1 Answers1

7

The first thing to check would be to make sure you are hydrating your React component where you’re using it. In Astro, components ship zero JS by default, just plain HTML. Components will only be interactive in the browser if you add a client:* directive. This is part of Astro’s “Islands Architecture”.

To include a component’s JS you need a client directive saying when to load it. In this example the component will load its JS when the page loads:

---
// src/pages/index.astro
import SecondSection from '../components/SecondSection.jsx';
---
<SecondSection client:load />

There are different directives like client:idle or client:visible that you can use to control exactly when a user needs the interactivity. There’s more about the client directives in Astro’s docs.

swithinbank
  • 1,127
  • 1
  • 6
  • 15