DEV Community

Discussion on: New Redux 😱 is just 🔥

Collapse
 
valeriavg profile image
Valeria

I feel that Redux articles at this one in particular aren't showing what Redux should be used for.
For the provided example, as well as almost any React project, React's useReducer would be a much better option:

import React, { useReducer } from "react";

export default function App() {
  const [counter, dispatch] = useReducer(
    (state, action) =>
      action === "+" ? state + 1 : action === "-" ? state - 1 : state,
    0
  );

  return (
    <div>
      <button aria-label="Increment value" onClick={() => dispatch("+")}>
        Increment
      </button>
      <span>{counter}</span>
      <button aria-label="Decrement value" onClick={() => dispatch("-")}>
        Decrement
      </button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

And if you need to pass the state along the app, add a React context to the mix.
So why should one add Redux to the project if React already comes shipped with similar tools?

Collapse
 
iakovosvo profile image
iakovosvo

You definitely don't need redux for a counter app but once your app starts growing and getting more complex you might end up implementing redux yourself.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic • Edited

React is soon getting a useSelector hook in core which makes redux useless.

Thread Thread
 
phryneas profile image
Lenz Weber

That has been in discussion in years. It might happen, but I would not make any bets on how the final Api will look or write code anticipating it today.

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic

Until then Valtio is the cleanest solution out there no competition.

Collapse
 
valeriavg profile image
Valeria

Not really, in most cases you're better off with scoped states for a complex app. It makes it much easier to test and maintain.

I think you really need Redux when you need a state that is mutated from a lot of places, so that it makes sense to make it global, or if you want to track the global state of the whole app for the time travel.