DEV Community

React - Redux In Less Then 7 Minutes

Matt Eddy on March 17, 2021

Attention!!! The code shown in this article is a valid implementation of the redux pattern referenced from the Official Redux.js.org. ...
Collapse
 
markerikson profile image
Mark Erikson

Hi, I'm a Redux maintainer. Please note that "modern Redux" code is very different than what most older tutorials show. We've introduced newer APIs like Redux Toolkit, which is a set of utilities that provide a light abstraction to simplify the most common Redux tasks, and the React-Redux hooks API, which is generally easier to use than the traditional connect API.

I strongly recommend reading through the newly rewritten official tutorials in the Redux docs, which have been specifically designed to teach you how Redux works and show our recommended practices:

  • "Redux Essentials" tutorial: teaches "how to use Redux, the right way", by building a real-world app using Redux Toolkit
  • "Redux Fundamentals" tutorial: teaches "how Redux works, from the bottom up", by showing how to write Redux code by hand and why standard usage patterns exist, and how Redux Toolkit simplifies those patterns

The older patterns shown in almost all other tutorials on the internet are still valid, but not how we recommend writing Redux code today.

You should also read through the Redux "Style Guide" docs page, which explains our recommended patterns and best practices. Following those will result in better and more maintainable Redux apps.

In addition, the easiest way to start a new project is with the official Redux+JS template for Create-React-App. It comes with Redux Toolkit and the React-Redux hooks API already set up when the project is created.

Collapse
 
meddy672 profile image
Matt Eddy

Also how do you become an active contributor of the redux api? I think this would be valuable information for those who are looking for ways to get involved with an open source project. May we connect with directly?

Collapse
 
markerikson profile image
Mark Erikson

I'm always interested in having folks contribute to Redux! Now, having said that: the Redux core library is stable and there's no active development work going on there. Same with React-Redux for now.

There is some active development work going on with Redux Toolkit, but it's primarily myself and a couple other maintainers atm.

Beyond that, there's a ton of work that I want to do with the Redux docs that I don't have time to do all by myself:

github.com/reduxjs/redux/issues/3592

and I'd definitely love to have folks get involved with that effort. (I actually got started with Redux by contributing the Redux FAQ to the docs back in early 2016.)

I hang out in the #redux channel in the Reactiflux Discord ( reactiflux.com ). Feel free to drop by and say hi.

Thread Thread
 
meddy672 profile image
Matt Eddy

I will check this out myself and also advertise this on this article.

Collapse
 
meddy672 profile image
Matt Eddy

I was unable to access the discord channel, but I would be happy to knock out a few of those issues you have on your repository. Do we just fork the repository and work the issues as described?

Collapse
 
markerikson profile image
Mark Erikson

Hmmm. Anyone should be able to sign up for a Discord account if you don't have one yet, and then join the Reactiflux discord. We do have a welcome flow where you have to agree to the community guidelines before you can access any other channels, so make sure you tried that.

If there's a particular issue you're interested in working on, leave a comment in that issue thread and we can discuss details.

Thread Thread
 
meddy672 profile image
Matt Eddy • Edited

I was able to get access to the channel taking a moment to review the welcome content and I have left a comment on the repo. #3591

Collapse
 
meddy672 profile image
Matt Eddy • Edited

Thank you for the feedback. How old are the methods used in this tutorial compared to that of the redux toolkit?

Collapse
 
markerikson profile image
Mark Erikson

The patterns shown here are the original Redux usage patterns that we've shown since day 1: hand-written immutable update logic, SCREAMING_SNAKE_CASE constants, separate "actions" and "reducers" files, and use of connect.

The pieces that we now call "modern Redux" have come out over time:

  • The React-Redux hooks API was launched in React-Redux v7.1, in June 2019
  • The Redux "Style Guide" page was published in fall 2019
  • Redux Toolkit 1.0 was released in October 2019
  • Last year I rewrote the Redux docs tutorials from scratch. I published the "Essentials" tutorial around June, and the "Fundamentals" tutorial at the end of October
Collapse
 
joelpatrizio profile image
Joel Patrizio

Awesome, thanks for sharing!

Collapse
 
adambiggs profile image
adam-biggs

Thanks for the post Matt!

One of the best and most overlooked alternatives to Redux is to use React's own built-in Context API.

Context API provides a different approach to tackling the data flow problem between React’s deeply nested components. Context has been around with React for quite a while, but it has changed significantly since its inception. Up to version 16.3, it was a way to handle the state data outside the React component tree. It was an experimental feature not recommended for most use cases.

Initially, the problem with legacy context was that updates to values that were passed down with context could be “blocked” if a component skipped rendering through the shouldComponentUpdate lifecycle method. Since many components relied on shouldComponentUpdate for performance optimizations, the legacy context was useless for passing down plain data.

The new version of Context API is a dependency injection mechanism that allows passing data through the component tree without having to pass props down manually at every level.

The most important thing here is that, unlike Redux, Context API is not a state management system. Instead, it’s a dependency injection mechanism where you manage a state in a React component. We get a state management system when using it with useContext and useReducer hooks.

A great next step to learning more is to read this article by Andy Fernandez: scalablepath.com/react/context-api...