Next.js Discord

Discord Forum

Why use `useEffect` to avoid hydration error?

Answered
piscopancer posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by piscopancer
I seem to understand what's going on. The mismatch between server and client lists occurs because this function runs twice - once by server, once by client - and as prepareCards shuffles the list, the results differ
View full answer

3 Replies

Avatar
piscopancer
If I apply logic to conclude that the following code is correct, Next throws hydr error at me
const [cards, setCards] = useState<TMemoGame['cards']>(prepareCards(props.cards))
I hate it because I am having an aesthetic issue as cards do not instantly appear on the page, only is the js loaded they will also appear. It makes layout shift
Avatar
piscopancer
I seem to understand what's going on. The mismatch between server and client lists occurs because this function runs twice - once by server, once by client - and as prepareCards shuffles the list, the results differ
Answer