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 building a dashboard layout. On desktop I want a fixed left sidebar (250px), on mobile it should slide in/out as a drawer.
I've tried using hidden and flex breakpoint utilities but can't get the transition to work smoothly. Is there a standard Tailwind pattern for this?
Priya Nair
asked 1 months ago2
Answers23
Upvotes3422
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."
Lena Müller
asked 1 months ago1
Answers114
Upvotes4100
ViewsI want to automatically run tests and type-checking before each deployment to Vercel. Currently I just push to main and Vercel deploys automatically, but sometimes broken code gets deployed.
My stack: Next.js 14, TypeScript, Vitest for tests. How do I set up GitHub Actions to block the deployment if tests fail?
Tom Kowalski
asked 1 months ago1
Answers18
Upvotes3100
ViewsI 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?
David Kim
asked 2 months ago1
Answers88
Upvotes6300
ViewsMy 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?
Tom Kowalski
asked 2 months ago2
Answers88
Upvotes7800
ViewsI want to add a dark mode toggle to my Next.js app using Tailwind's built-in dark mode support. I want to:
What's the correct implementation? Should I use next-themes or roll my own?
Carlos Ribeiro
asked 2 months ago1
Answers103
Upvotes9200
ViewsMy Express API started as a simple project but has grown to 50+ routes. The codebase is getting messy. Currently everything is in one app.js file.
What's the recommended folder structure for a production-grade Express API? Should I be looking at something like NestJS at this scale?
Alex Martinez
asked 2 months ago1
Answers46
Upvotes4400
ViewsI have two collections: orders and customers. I want to fetch all orders along with the customer's name and email.
// orders: { _id, customerId, total, status }
// customers: { _id, name, email }
I tried $lookup but it's killing performance on large datasets (~2M orders). Should I be using indexes differently, or is there a better aggregation strategy?
Priya Nair
asked 2 months ago1
Answers51
Upvotes5670
ViewsI'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?
Sofia Greco
asked 2 months ago1
Answers14
Upvotes6700
Views1