DEV Community

Discussion on: Redux: Switching to other case within the same reducer

Collapse
 
vonheikemen profile image
Heiker

The quick way that I can think of is using recursion.

if(!isMovementFeasible(state, action.payload)) {
  return movementReducer(
    state,
    { type: 'moveObjectError', payload: 'Nop' }
  );
}

const new_position= ChangePosition(action.payload);
return {
  ...state,
  position: new_position  
}
Enter fullscreen mode Exit fullscreen mode

The "good" way would be creating a state machine inside your reducer. The good news is that the action.type can act as an event, so you only need to add a label to your state to make it work. The bad news is that it would involve some nesting conditionals so it may not look "elegant".