Skip navigation
❌ Avoid deriving state in useEffect in React, this can create multiple unnecessary render phases.
0:00 / 0:29

❌ Avoid deriving state in useEffect in React, this can create multiple unnecessary render phases.

George Moller

@_georgemoller119K followers

Visit on X
444 likesDownloadOriginal post

33,474 views

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

11 Comments

Comments from the original post on X (Twitter)

  • Nikhil S's profile picture

    @_nikhilsnayak_

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

    6 likes

  • Christian Esmann's profile picture

    @chrisesmann

    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.

    137 likes

  • mikhaylovich's profile picture

    @mikhaylovich_

    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)

    4 likes

  • Fabrizio's profile picture

    @ipla03

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

    4 likes

  • Ryan Faust's profile picture

    @RyanRFaust

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

    2 likes

  • Toyyib 💙's profile picture

    @dev_toykam

    @Rhoy__b @ibr9heem

    1 like

  • Ali Bey's profile picture

    @AliHussein_20

    I avoid useEffect whenever I can

    1 like

  • Travis Watson's profile picture

    @traviswatsonws

    useMemo is gonna blow your mind

    1 like

  • Mmina_SCRM Solutions's profile picture

    @Wwwadesk

    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.

    1 like

  • shubham Gupta's profile picture

    @sg43245

    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's profile picture

    @7792Tna

    The official documentation explains this in detail.