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.
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.
Share Improve this question edited Mar 1, 2024 at 9:11 VLAZ 29k9 gold badges62 silver badges83 bronze badges asked Nov 25, 2022 at 22:42 cucereanumcucereanum 2233 silver badges10 bronze badges2 Answers
Reset to default 19The 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.
I had a similar issue where by FAQ.tsx
file with interactive dropdown wasn't working. My problem was that my astro.config.mjs
file had react listed twice in the integrations section:
export default defineConfig({
site: "https://codeontherocks.dev",
integrations: [
tailwind(),
react({
include: ['**/react/*'],
}),
react(), <-- Remove this
sitemap(),
astroExpressiveCode(),
mdx(),
],
});
After removing the react entry without the specified file path everything started working again.