Sitemap

useEffect() and Love Letters?!

3 min readFeb 15, 2022

--

Has anyone ever drafted an extensive, multi-paragraph response pouring their heart out to their long-lost lover about why things couldn’t be ten years ago (because your high school self only had eyes for Sean Faris)

only to accidentally refresh the page and now all of your heart-wrenching words are gone, never to be read, perhaps for the best?

No? Yeah… me neither… I mean, who does that…

Get Rebekah Zhou’s stories in your inbox

Join Medium for free to get updates from this writer.

Since I’m sure you would agree that we can’t let that hard work be washed down the drain, let me show you how you can help others avoid such a blunder (not the letter itself… I mean losing their draft, of course).

The Version Where We Lose The Letter On Refresh

Mess with the code here

The Version Where The User Can Save Their Letter On Refresh

Mess with the code here

The main differences in words:

  1. Import useEffect
  2. Change the initialization of our text state variable from “” to localStorage.getItem(“insert-anything-here-even-potato”)
  3. Add the useEffect() hook inside our App component, which takes in a callback function with localStorage.setItem(“insert-anything-here-even-potato”) inside, and a dependencies array: [text]

Let’s break down the useEffect hook a bit more

The useEffect hook lets us do side effects inside our function components. So after our component renders, we can have useEffect perform data fetching, manually manipulating the DOM, using a timer, etc.

useEffect(callbackFunction, dependencies)

Inside our useEffect() hook is a callback function that contains our side effect code. In our example above, our callback function has localStorage.setItem(‘love-letter’, text), which allows us to store our data from the text state variable into our localStorage as a key-value pair. Thus, we have our key ‘love-letter’ and the value being whatever the user types into the input box (since text state variable contains the user input via the onChange event handler).

Dependencies

After our callback function we have a dependencies argument. Dependencies may contain an array that the useEffect hook refers to in order to trigger the side effect. Since our example has [text] as its dependencies argument, this means that our useEffect hook is looking to see if text has been updated. After the component re-renders, the useEffect hook runs the side effect again. We can set different arguments as dependencies to allow us to control when the side effect runs:

  1. An array containing state/prop values [text, prop1, prop2, …]. We can add more values if desired so that the side-effect runs when any of the dependency values change.
  2. An empty array []. This will have the useEffect run only once after the initial rendering.
  3. Nothing. The side effect will run after every render.

useEffect Tips

  • If you want to implement several side effects, then don’t be afraid to use multiple useEffect hooks.
  • Got an async function? Throw it into useEffect.

TL;DR

You can use the Effect Hook in react components to ensure that your cringy love letter doesn’t get lost when you refresh the page.

Be sure to check out the React Docs on the Effect Hook!

--

--

Rebekah Zhou
Rebekah Zhou

Written by Rebekah Zhou

Full Stack Software Engineer. Former Math Teacher, Lifelong Math Enthusiast.