DEV Community

Gahyun Son
Gahyun Son

Posted on

useContext vs Redux toolkit

I'm a newbie in Redux and React. Recently I've been developing a project with React, and I'm working on structuring the user authentication API and managing user information using Redux. I need to deepen my knowledge of Redux and React because it's quite challenging for a beginner.
I found an article about Handling global state. This article introduces various options for managing global state.
One of things that I wanted to explore was the 'context'. Below are some simple code examples to check the difference between React context and Redux.

useContext (React)

  1. Declaring the Context.

First, I declare a context using createContext. It is the default value. And you can set a value with useState if you want(If you don't declare value then default value will be used)

const ThemeContext = createContext('light');

const Main = () => {
  const [theme, setTheme] = useState('light');
Enter fullscreen mode Exit fullscreen mode
  1. Providing a value in ThemeContext.Provider. 'theme' or everything is good. but if you need a dynamic value, you should use useState.
<ThemeContext.Provider value={'theme'}>
Enter fullscreen mode Exit fullscreen mode
  1. Using Global state? In this case, accessing {theme} inside the Panel component works well. Because Panel is a child of Main. I tried changing the value, and it updated correctly on screen.
function Panel({ title, children }) {
  const theme = useContext(ThemeContext);
  const className = 'panel-' + theme;
  return (
    <section className={className}>
      <h1>{title} and {theme}</h1>
      {children}
    </section>
  )
}
Enter fullscreen mode Exit fullscreen mode

I'd tried here too. The initial value was displayed correctly, but when i tried changing it on screen, it didn't update. This happens because Test is not relevant with Main or Panel anything (not within the ThemeContext.Provider). So it only show us the default value or initial value.

function Test() {
  const theme = useContext(ThemeContext);
  return (
    <p>This is {theme}</p>
  )
}
Enter fullscreen mode Exit fullscreen mode

Redux toolkit

I used Redux toolkit, It's very simple calling global state.

const count = useSelector(state => state.counter.value)
Enter fullscreen mode Exit fullscreen mode

You just add it to a component.
The components don't have to have a relevant. You don't have to pass a prop.
I could see the changing value when I click the button everywhere I insert above code.


So some articles say React Context is not for managing global state. OK I understand now. If I try to use react context as managing global state, I will have headache. I'm so happy that I'm using Redux.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay