When Redux is used to incorporate the apps state in React & React Native, why is a type required in an action creator but a payload is not required?
What purpose does an action creator serve if no payload is attached to the action?
When Redux is used to incorporate the apps state in React & React Native, why is a type required in an action creator but a payload is not required?
What purpose does an action creator serve if no payload is attached to the action?
Sometimes you have a reducer that does not return a new state based on a payload. An example would be an action that toggles something in the state. The reducer would just need to know that the action was fired to toggle a property. Example:
const lightSwitch = (
state = {on: false},
action,
) => {
switch (action.type) {
case TOGGLE:
return { ...state, on: !state.on };
default: return state;
}
}
One of the most popular Redux action type is for a fetching/loading indicator action. For example:
export default (state = { isFetching: false }, action) => {
switch(action.type) {
case START_FETCHING_POSTS:
return { ...state, isFetching: true };
case STOP_FETCHING_POSTS:
return { ...state, isFetching: false };
default:
return state;
}
};
In this case, the action will look like this const action = { type: START_FETCHING_POSTS }
render(){
this.props.navigation.addListener(
'didFocus',
payload => {
console.log("Payload is called .....................")
}
);
}