DEV Community

Cover image for Learning React Even on a “Busy Holiday” – Saving Data to Local Storage with useEffect
Usama
Usama

Posted on

Learning React Even on a “Busy Holiday” – Saving Data to Local Storage with useEffect

Today I’m going home on leave.
But honestly, this is not a formal holiday.

I’m not free.
I have an important responsibility waiting for me, and I already know it’s going to take energy and focus. Still, I didn’t want this break to become an excuse for breaking my React learning rhythm.

That’s something I’ve learned the hard way:
momentum is fragile.

So before leaving, I made a small but intentional decision — I watched a React lecture.
Not passively, not casually — consciously.

The lecture was about saving data to localStorage in React, using state, useEffect, and state callback patterns.
I haven’t done the practical yet, but InshaAllah, as soon as I get off the train, freshen up, I’ll sit down and implement everything properly.

This blog is my way of locking the concept in my mind — and documenting my journey.


Why Local Storage Matters in Real React Apps

When we start with React, most examples feel temporary.

You refresh the page → everything disappears.

But real applications don’t work like that.

  • User preferences
  • Theme settings
  • Watched movies
  • Cart items
  • Form drafts

All of these must survive a page reload.

That’s where localStorage comes in.

It allows the browser to store data persistently, even after refresh or browser restart.


The Real Challenge: React State vs Local Storage

Here’s something that confused me earlier:

“If I already have state, why do I need localStorage?”

The answer is simple but powerful:

  • React state → lives in memory (temporary)
  • localStorage → lives in the browser (persistent)

The goal is not to replace state —
the goal is to sync state with localStorage.


Step 1: Initializing State from Local Storage

Instead of hardcoding default values, we can read from localStorage first.

This is where the state callback function becomes important.

Example idea (not full code):

const [items, setItems] = useState(() => {
  const stored = localStorage.getItem("items");
  return stored ? JSON.parse(stored) : [];
});
Enter fullscreen mode Exit fullscreen mode

Why this matters:

  • The function runs only once
  • Prevents unnecessary localStorage reads on every render
  • Makes the app load with saved data automatically

This felt like a professional React habit to me.


Step 2: Keeping Local Storage in Sync with State

This is where useEffect shines.

Whenever the state changes, we want to update localStorage.

Mental model:

“State is the source of truth.
localStorage is just a mirror.”

Conceptually:

useEffect(() => {
  localStorage.setItem("items", JSON.stringify(items));
}, [items]);
Enter fullscreen mode Exit fullscreen mode

Key learning here:

  • useEffect runs after render
  • Dependency array ensures it runs only when state changes
  • Keeps browser storage always updated

This pattern clicked for me today.


A Small but Important Detail: JSON

localStorage only stores strings.

That means:

  • Objects → JSON.stringify()
  • Arrays → JSON.stringify()
  • Reading back → JSON.parse()

Ignoring this detail leads to bugs that feel “mysterious” at first.

Now I know better.


Why I’m Proud of Today (Even Without Practical… Yet)

I didn’t finish everything today.

And that’s okay.

What matters is:

  • I protected my learning flow
  • I didn’t skip React
  • I made a clear plan to do the practical as soon as I reach home

Consistency isn’t about perfect days. It’s about showing up even on imperfect ones.


Final Thoughts

This lecture reminded me that React isn’t just about components and JSX.

It’s about:

  • thinking in data flow
  • understanding browser behavior
  • writing code that survives real user actions

Saving data to localStorage using useState callbacks and useEffect is a small topic —
but it’s a big step toward real-world React development.

InshaAllah, once I complete the practical, I’ll come back stronger and move forward without breaking momentum.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.