Alexis Wilke
Alexis Wilke

Reputation: 20720

React-Redux example has a non-pure function. I thought reducers had to be pure?

Redux says that actions handling the state (Reducers) have to be pure.

Looking at the React-Redux docs, though, they show how to add a new Todo task to the list of Todos and the function generates a new identifier for the new Todo task. This is obviously not pure. Calling the same function again will not result in the same output.

There is the example (from this page):

// redux/actions.js
import { ADD_TODO } from "./actionTypes";

let nextTodoId = 0;
export const addTodo = content => ({
  type: ADD_TODO,
  payload: {
    id: ++nextTodoId,
    content
  }
});

// ... other actions

Does that means I have to forget everything I learned so far?

Upvotes: 0

Views: 277

Answers (1)

TrueWill
TrueWill

Reputation: 25523

It looks to me like that's an action creator, not a reducer. Action creators don't have to be pure.

Action creators can also be asynchronous and have side-effects.

https://redux.js.org/basics/actions#action-creators

Upvotes: 5

Related Questions