Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

❌ Avoid deriving state in useEffect in React, this can create multiple unnecessary render phases. ✅ Instead derive the state directly in the event handler, this way you'll have one single render phase. ↓

33,474 Aufrufe • vor 1 Jahr •via X (Twitter)

11 Kommentare

Profilbild von Nikhil S
Nikhil Svor 1 Jahr

A more simple approach in this case is to derive the value during render const isValid = isYearOfBirthValid(yearOfBirth)

Profilbild von Christian Esmann
Christian Esmannvor 1 Jahr

I just launched an all-in-one template for cross-platform development, based on the stack I use myself everyday. Expo, NextJS, TypeScript, Tailwind, Firebase, AppsFlyer, Authentication, Analytics, In-App Purchases, Stripe and a lot more, setup by default.

Profilbild von mikhaylovich
mikhaylovichvor 1 Jahr

is it valid to replace all of that (useEffect + useState) with an useMemo? it still tracks each change on yearOfBirth, applies the validation, returns 'isValid' but also drops the extra useState on isValid and it's much cleaner (also setYearOfBirth is moved to onChange)

Profilbild von Fabrizio
Fabriziovor 1 Jahr

This is as much as an antipattern as the useEffect solution, just derive during the render and add useMemo if needed (only if needed)

Profilbild von Ryan Faust
Ryan Faustvor 1 Jahr

As a self learned coder, I didn't know this. Or many other best practices. Your content is gold for me.

Profilbild von Toyyib 💙
Toyyib 💙vor 1 Jahr

@Rhoy__b @ibr9heem

Profilbild von Ali Bey
Ali Beyvor 1 Jahr

I avoid useEffect whenever I can

Profilbild von Travis Watson
Travis Watsonvor 1 Jahr

useMemo is gonna blow your mind

Profilbild von Mmina_SCRM Solutions
Mmina_SCRM Solutionsvor 1 Jahr

deriving state in event handlers is a smart move. it cuts down on extra renders, making your app run smoother. keep it efficient, especially in complex components.

Profilbild von shubham Gupta
shubham Guptavor 1 Jahr

This is a great insight. The animation in your post is super clean—may I ask which tool you used to create it?

Profilbild von tna 7792
tna 7792vor 1 Jahr

The official documentation explains this in detail.

Ähnliche Videos