Action arg in reducer comes from?

I can see action is from the loginAction function, but that function does not call reducer. I think the createStore method calls the reducer function, but without arguments passed. so what part of the code is passing action into reducer ( or how does reducer know that action=loginAction() )?


const defaultState = {
login: false
};

const reducer = (state = defaultState,  *action*) => {
// Change code below this line
if(action.type === 'LOGIN'){return {login: true}}
else{return {login: false}};
// Change code above this line
};

const store = Redux.createStore(reducer);

const loginAction = () => {
return {
  type: 'LOGIN'
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36

Challenge: Handle an Action in the Store

Link to the challenge:

Hi @danCamalMan
following reduce logic can be confusing at first.

The loginAction function creates an action object when called(it returns it). The createStore method does not call the reducer, it only takes it as an argument, by which it would mold the reduce store. There is difference between myFunction and myFunction(). The first only refer to the function, the second will call it and have on its place the value it returns.
The reducer function defines the different actions and how the stored state is to be handled, based on the action passed. It doesnt know what actions you would pass, but it can only work with the ones it knows to handle. In the next challenges, you will see how when you work with the store, you will pass actions to the reducer. So far you only defined the store, actions etc. but you havent put them into use

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.