Lädt...

🔧 useContext vs Redux toolkit


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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');
  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'}>
  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>
  )
}

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>
  )
}

Redux toolkit

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

const count = useSelector(state => state.counter.value)

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.

...

🔧 Redux VS Redux Toolkit &amp;&amp; Redux Thunk VS Redux-Saga


📈 58.97 Punkte
🔧 Programmierung

🔧 Guide to Redux, React-Redux, and Redux Toolkit


📈 46.58 Punkte
🔧 Programmierung

🔧 Redux Toolkit Advanced: Scaling Redux Toolkit for Large Applications


📈 43.59 Punkte
🔧 Programmierung

🔧 useContext vs Redux toolkit


📈 43.42 Punkte
🔧 Programmierung

🔧 Redux Toolkit Tutorial || React Redux || TypeScript


📈 34.19 Punkte
🔧 Programmierung

🔧 React Redux Toolkit Course For Beginners - How To Build Using Redux


📈 34.19 Punkte
🔧 Programmierung

🔧 🚀 Creating a Feature-Rich Redux Store with Redux Toolkit and TypeScript


📈 34.19 Punkte
🔧 Programmierung

🔧 Learn Redux and Redux Toolkit for State Management


📈 34.19 Punkte
🔧 Programmierung

🎥 Redux and Modern Redux Toolkit with Asynchronous Operation – Full Course


📈 34.19 Punkte
🎥 Video | Youtube

🔧 Redux: Intro + Building a Modern State Management System with Redux Toolkit


📈 34.19 Punkte
🔧 Programmierung

🔧 Counter example in all the four ways: Prop Drilling, Context API, Redux, and Redux Toolkit.


📈 34.19 Punkte
🔧 Programmierung

🔧 Mastering Redux Toolkit: Building a Task Management App | React &amp; Redux


📈 34.19 Punkte
🔧 Programmierung

🔧 Expo with Redux Toolkit, File System, and Redux Persist: A Comprehensive Guide


📈 34.19 Punkte
🔧 Programmierung

🔧 The Dynamic Trio: React, Redux Toolkit, and Redux Saga 🚀


📈 34.19 Punkte
🔧 Programmierung

🔧 Redux vs. Redux Toolkit: A Comprehensive Guide


📈 34.19 Punkte
🔧 Programmierung

🔧 Exploring Redux Toolkit 2.0 and the Redux second generation


📈 34.19 Punkte
🔧 Programmierung

🔧 Why Redux Toolkit Outshines Traditional Redux for Modern State Management? 🚀


📈 34.19 Punkte
🔧 Programmierung

🔧 Setting up Redux Persist with Redux Toolkit in React JS


📈 34.19 Punkte
🔧 Programmierung

🔧 Getting Started with Redux and Redux Toolkit


📈 34.19 Punkte
🔧 Programmierung

🔧 useContext vs. Redux: Which One Should You Use? 🤔


📈 34.02 Punkte
🔧 Programmierung

🔧 useContext + useReducer vs Redux


📈 34.02 Punkte
🔧 Programmierung

🔧 A Comprehensive Guide to Redux and Redux-Saga for Beginners


📈 24.79 Punkte
🔧 Programmierung

🔧 Is Redux Dead? Why I Kicked Redux Out of Our SaaS App


📈 24.79 Punkte
🔧 Programmierung

🔧 Implementing Redux with Redux Persist in a Next.js App Router Application


📈 24.79 Punkte
🔧 Programmierung

🔧 Implementing Redux with Redux Persist in a Next.js App Router Application


📈 24.79 Punkte
🔧 Programmierung

🔧 Implementing Redux and Redux-Thunk in a React Native Application


📈 24.79 Punkte
🔧 Programmierung

🎥 React Redux Tutorial #1 - Was ist React Redux


📈 24.79 Punkte
🎥 IT Security Video

🎥 React Redux Tutorial #1 - Was ist React Redux


📈 24.79 Punkte
🎥 IT Security Video

🔧 Connecting Redux Form with React Redux


📈 24.79 Punkte
🔧 Programmierung

🔧 Connecting Redux Form with React Redux


📈 24.79 Punkte
🔧 Programmierung

🔧 Learn Redux Toolkit - React (TypeScript)


📈 21.79 Punkte
🔧 Programmierung

🔧 Understand Redux Toolkit Query and its Applications in Web App Development


📈 21.79 Punkte
🔧 Programmierung