I’m working on a Next.js project and I want to understand the most correct way to fetch API data in a page using server-side rendering.
I tried using useEffect, but the initial content is not available immediately and I’d like to avoid SEO issues and hydration mismatches.
What is the recommended approach between Server Components, getServerSideProps, and client-side fetching?
Are there cases where one is better than the others?
Matteo Marconi
asked 1 months ago0
Answers0
Upvotes2
ViewsI'm setting up testing for my React app using Vitest + React Testing Library. I'm not sure about best practices:
vi.mock()?Looking for practical examples, not just "test behavior not implementation."
I wrote a custom hook for fetching data. Is this the right way to do it, or am I missing something?
function useFetch(url: string) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch(url)
.then(res => res.json())
.then(setData)
.catch(setError)
.finally(() => setLoading(false));
}, [url]);
return { data, loading, error };
}
Should I use AbortController? What about race conditions?
My component keeps re-rendering infinitely. Here's a simplified version:
function MyComponent({ onUpdate }) {
const [data, setData] = useState(null);
useEffect(() => {
fetchSomething().then(setData);
onUpdate(data);
}, [onUpdate, data]);
}
I think the issue is with onUpdate but I'm not sure. What are the rules for the dependency array?
I'm migrating my Next.js app to use React Server Components (RSC). I love the performance benefits, but I'm confused about the boundary.
Specifically: I have a ProductList RSC that fetches products, but each product card needs a client-side "Add to Cart" button with optimistic UI. What's the recommended pattern?
I'm starting a new project and I'm torn between the App Router and the legacy Pages Router in Next.js 14.
My requirements:
Which router would you recommend and why? Are there known gotchas I should be aware of?
Lena Müller
asked 1 months ago1
Answers114
Upvotes4100
Views1
Answers88
Upvotes6300
Views2
Answers88
Upvotes7800
Views1
Answers14
Upvotes6700
Views3
Answers35
Upvotes8911
Views