Загрузка видео...

Не удалось загрузить видео

На главную

❌ 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 просмотров • 1 год назад •via X (Twitter)

Комментарии: 11

Фото профиля Nikhil S
Nikhil S1 год назад

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

Фото профиля Christian Esmann
Christian Esmann1 год назад

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.

Фото профиля mikhaylovich
mikhaylovich1 год назад

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)

Фото профиля Fabrizio
Fabrizio1 год назад

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

Фото профиля Ryan Faust
Ryan Faust1 год назад

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

Фото профиля Toyyib 💙
Toyyib 💙1 год назад

@Rhoy__b @ibr9heem

Фото профиля Ali Bey
Ali Bey1 год назад

I avoid useEffect whenever I can

Фото профиля Travis Watson
Travis Watson1 год назад

useMemo is gonna blow your mind

Фото профиля Mmina_SCRM Solutions
Mmina_SCRM Solutions1 год назад

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.

Фото профиля shubham Gupta
shubham Gupta1 год назад

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

Фото профиля tna 7792
tna 77921 год назад

The official documentation explains this in detail.

Похожие видео