viphat / til

Today I Learned
http://notes.viphat.work
0 stars 1 forks source link

[React + Redux] - Reducers #296

Open viphat opened 6 years ago

viphat commented 6 years ago

A Reducer describes how an application's state changes. You'll often see the Object Spread Operator (...) used inside of a reducer because a reducer must return a new object instead of mutating the old state.

If you want to know why Redux requires immutability, check out the this document - https://redux.js.org/faq/immutable-data#why-is-immutability-required

Reducers have the following signature:

(previousState, action) => newState

The tweets reducer will determine how the tweets part of the state changes and the users reducer will determine how the users part of the state changes, and so forth.

Initializing State

There are 2 ways to initialize the state inside the store:

const store = createStore (
  rootReducer,
  { tweets: {} }
);
function tweets (state = {}, action) {
}

https://redux.js.org/recipes/structuring-reducers/initializing-state