"You're importing a component that needs useEffect" - ERROR #59857
-
SummaryHi! ./src\CountriesList.js You're importing a component that needs useEffect. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default. Learn more: https://nextjs.org/docs/getting-started/react-essentials ╭─[C:\Users\simon\Downloads\Uppgift3\labb3\src\CountriesList.js:1:1] Maybe one of these should be marked as a client entry with "use client": Please help! Additional informationMy CountriesList.js code:
import React, { useState, useEffect } from 'react';
import fetch from 'isomorphic-unfetch';
const CountriesList = () => {
const [countries, setCountries] = useState([]);
useEffect(() => {
const fetchCountries = async () => {
try {
const response = await fetch('https://restcountries.com/v3.1/all');
const data = await response.json();
setCountries(data);
} catch (error) {
console.error('Error fetching countries data:', error);
}
};
fetchCountries();
}, []);
return (
<div>
<h2>List of Countries</h2>
<ul>
{countries.map((country) => (
<li key={country.cca2}>{country.name.common}</li>
))}
</ul>
</div>
);
};
export default CountriesList;
--------------------------
The code for page.js:
import React from 'react';
import CountriesList from '@/CountriesList'; // Uppdatera sökvägen här
const Home = () => {
return (
<div>
<h1>My Next.js App</h1>
<CountriesList />
</div>
);
};
export default Home; ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
I hope it helps! |
Beta Was this translation helpful? Give feedback.
-
Try to write |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
To whom it may concern, I forgot to add |
Beta Was this translation helpful? Give feedback.
CountriesList.{js | jsx}
needs to be a client component as it uses functions that need the browser enviroment (useState, UseEffect and fetch). And there's no problem to import a client component in a server one!I hope it helps!