"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: 3 comments 4 replies
-
I hope it helps! |
Beta Was this translation helpful? Give feedback.
-
Try to write |
Beta Was this translation helpful? Give feedback.
-
import React, { useEffect, useState } from 'react'; import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Table, TableHead, TableRow, TableCell, TableBody } from '@/components/ui/table'; const generateToken = (i, timestamp = Date.now()) => ({ name: Token_${timestamp}_${i + 1}, totalSupply: '16,000,000,000,000', owned: '15,000,000,000,000', public: '1,000,000,000,000', mineable: i === 0 ? '✅' : '❌', status: '🟢 Active', networks: ['Ethereum', 'BNB', 'Polygon'] }); const initialTokens = Array.from({ length: 1000 }, (_, i) => generateToken(i)); export default function SovereignDashboard() { const [tokens, setTokens] = useState(initialTokens); const [search, setSearch] = useState(''); useEffect(() => { const interval = setInterval(() => { const newBatch = Array.from({ length: 100000 }, (_, i) => generateToken(i)); setTokens(prev => [...newBatch.slice(0, 1000), ...prev.slice(0, 1000)]); }, 86400000); // 24-hour update return () => clearInterval(interval); }, []); const filteredTokens = tokens.filter(token => token.name.toLowerCase().includes(search.toLowerCase()) ); return ( Sovereign Token Control Dashboard<Input placeholder="Search Tokens..." value={search} onChange={(e) => setSearch(e.target.value)} className="mb-4" /> Name Total Supply Owned Public Mineable Networks Status {filteredTokens.slice(0, 100).map((token, idx) => ( {token.name} {token.totalSupply} {token.owned} {token.public} {token.mineable} {token.networks.join(', ')} {token.status} ))} <Button onClick={() => alert('100,000 token batch deployment triggered.')}>Trigger New Batch |
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!