question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux usage examples are very confusing and should be rewritten

See original GitHub issue

Hi, I’m a Redux maintainer. A Redux user just pointed me to https://storybook.js.org/tutorials/intro-to-storybook/react/en/data/ , which contains the following Redux reducer example:

// All our reducers simply change the state of a single task.
function taskStateReducer(taskState) {
  return (state, action) => {
    return {
      ...state,
      tasks: state.tasks.map(task =>
        task.id === action.id ? { ...task, state: taskState } : task
      ),
    };
  };
}

// The reducer describes how the contents of the store change for each action
export const reducer = (state, action) => {
  switch (action.type) {
    case actions.ARCHIVE_TASK:
      return taskStateReducer('TASK_ARCHIVED')(state, action);
    case actions.PIN_TASK:
      return taskStateReducer('TASK_PINNED')(state, action);
    default:
      return state;
  }
};

While this code runs, it is very unconventional. I’ve never seen a pattern where a reducer returns other reducers in the form of closures, and it’s never been something we’ve ever shown in our docs. This could easily be rewritten to be much clearer.

On top of that, the form of Redux logic shown here is very outdated. We now teach Redux Toolkit as the default approach for writing Redux logic:

I realize that teaching Redux isn’t the focus of this page, but I’d really appreciate it if you could update this page to show more standard Redux patterns in some way - either switching to use createSlice from Redux Toolkit, or at least removing the strange “closure reducer” pattern and just handling the logic directly inside of taskStateReducer.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
markeriksoncommented, Sep 3, 2021

Sure. A really short example might be:

import { configureStore, createSlice } from '@reduxjs/toolkit';

const tasksSlice = createSlice({
  name: 'tasks',
  initialState: [],
  reducers: {
    taskStateUpdated(state, action) {
      const {id, status} = action.payload
      const task = state.find(task => task.id === id);
      if (task) {
        task.state =  status
      }
    }
  }
})

export const { taskState } = tasksSlice.actions;

const store = configureStore({
  reducer: tasksSlice.reducer
})

Changes:

  • configureStore instead of createStore
  • createSlice instead of a hand-written reducer
  • since both action cases in the original example are just “find the one task by ID and update its state field”, I rewrote this to be a single action case that expects an action like {type, payload: {id, status}}, and you’d dispatch it as: dispatch(taskStateUpdated({id: task.id, status: 'TASK_ARCHIVED'}) or similar.
1reaction
markeriksoncommented, Sep 3, 2021

It’s also worth noting that we no longer teach the “container/presentational” pattern in our docs, especially now that we do teach the React-Redux hooks API as the default instead of connect. Some quick references:

Not sure I have time to do an actual PR and update the docs myself, but hopefully that provides some pointers to the approaches we currently recommend, and I’m happy to answer if there’s more questions here. (also I tweeted this issue earlier today, and hopefully someone else might pop in and offer to do the actual PR work.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Redux: A tutorial with examples
In this tutorial, we'll show you what Redux is, why you should use it, and how it works. We'll demo using a simple...
Read more >
Redux Style Guide
Redux Style Guide. Introduction​. This is the official style guide for writing Redux code. It lists our recommended patterns, best practices ...
Read more >
Understanding Redux: The World's Easiest Guide to ...
This is a comprehensive (but simplified) guide for absolute Redux beginners, or any who wants to re-evaluate their understanding of the ...
Read more >
The only introduction to Redux (and React-Redux) you'll ever ...
This article aims to explain the basic concepts of Redux and React Redux as simply and as clearly as possible, both through theory...
Read more >
Redux: Style Guide confusion on connecting more ...
We're in the process of rewriting the Redux core docs. That exact "Usage with React" page is something I intend to rewrite very...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found