Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Redux.js launches reducer function multiple times on init?

Learning Redux.js and building a demo app.

I have a reducer set like this:

// Imports here function blocksFunc(state = [], action) {    switch (action.type) {    case 'ADD_BLOCK':      _id++;     return [...state, {'_class' : 'basic', '_id' : _id }];    default:      state = [];      return state;   } }  const BlockGeneratorReducer = combineReducers({    blocksFunc, });  export default BlockGeneratorReducer; 

I successfully update the state, but when logging I get the following when page loads:

blocksFunc() type: "@@redux/INIT"

blocksFunc() type: "@@redux/PROBE_UNKNOWN_ACTION_b.f.4.q.y.o.a.v.2.t.9"

blocksFunc() type: "@@redux/INIT"

So blocksFunc function is launched three times with default action.type. On which occasions is the action type "@@redux/INIT" launched? What might "@@redux/PROBE_UNKNOWN_ACTIOM" refer in to?

The full source can be found on git: https://github.com/JaakkoKarhu/redux-react-blockgenerator

The working demo is uploaded to my server: http://jaakkokarhu.com/playground/redux-block-generator/

Since being new with React and Redux, all the other comments regarding the source are also very welcome.

EDIT:

blocksFunc() edited according to DavidWalshes advice.

like image 830
Jaakko Karhu Avatar asked Oct 27 '15 12:10

Jaakko Karhu


People also ask

Does Redux call all reducers?

One frequently asked question is whether Redux "calls all reducers" when dispatching an action. Since there really is only one root reducer function, the default answer is "no, it does not".

How many reducers should I use Redux?

Creating the Root Reducer​ A Redux app really only has one reducer function: the "root reducer" function that you will pass to createStore later on. That one root reducer function is responsible for handling all of the actions that are dispatched, and calculating what the entire new state result should be every time.

What is the main purpose of a reducer function in Redux?

Introduction. In Redux, a reducer is a pure function that takes an action and the previous state of the application and returns the new state. The action describes what happened and it is the reducer's job to return the new state based on that action.

What is the difference between actions and reducers Redux?

Reducers: As we already know, actions only tell what to do, but they don't tell how to do, so reducers are the pure functions that take the current state and action and return the new state and tell the store how to do.


1 Answers

@@redux/INIT is launched twice on purpose. First time is for testing combineReducers, second one is actual init: https://github.com/reactjs/redux/issues/382

As TenorB pointed out on question comments, @@redux/PROBE_UNKNOWN_ACTION is also launched for testing purpose.

So, after all, these events are not accidentally launched.

like image 129
Jaakko Karhu Avatar answered Oct 09 '22 15:10

Jaakko Karhu