正在加载视频...

视频加载失败

❌ 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.

相关视频