4

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?

Kelvan Ince
  • 88
  • 1
  • 6

3 Answers3

0

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;
  }
}
Yo Wakita
  • 5,322
  • 3
  • 24
  • 36
0

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 }

Vincent D'amour
  • 3,746
  • 1
  • 27
  • 39
0
render(){
 this.props.navigation.addListener(
    'didFocus',
       payload => {
         console.log("Payload is called .....................")
        }
  );

}
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53