useEffect and its
dependency array

Frontend Engineer

Cake Engineering - 03 March 2021

Agenda

  • What is useEffect?
  • An example of useEffect has bug 🐛
  • Find out the root cause and fix the bug
  • Closing notes

Recap

The Effect Hook lets you perform side effects in function components

useEffect(() => {
  //effect
  return () => {
    //cleanup
  }
}, [])

Recap

 A side effect is when a function alters some external state that is not the intended return value.

  • Data fetching
  • Setting up a subscription
  • Manually changing the DOM

Recap

  • Need clean up
  • Don't need clean up

React always clean up the previous effect before applying the next effect.

Requirements

Demo

Summary

  • Every time we re-render, we schedule a new effect, replacing the previous one.

 

  • Whenever the value inside the dependency array changes, React cleans up the previous effect and applies the new one.

Thanks Chee for your support on my React journey 🤓

Thank you!

useEffect and its dependency array

By Trung Vo

useEffect and its dependency array

Deck for Cake Engineering Conference #1 - March 2021 https://www.cakedefi.com/

  • 1,368