0

Is there any programmatical advantage of one of these approaches over the other?

actions.js

export const USER_CLOSED_DIALOG = 'USER_CLOSED_DIALOG';
export function userClosedDialog() {
  return (dispatch) => {
    dispatch(closeDialog());
  }
}

export USER_SAVED_DATA = 'USER_SAVED_DATA';
export function userSavedData() {
  return (dispatch) => {
    dispatch(closeDialog());
  }
}

export const CLOSE_DIALOG = 'CLOSE_DIALOG';
export function closeDialog() {
  return { type: CLOSE_DIALOG }
}

versus:

reducers.js

switch (action.type) {
  case USER_CLOSED_DIALOG:
  case USER_SAVED_DATA:
  case CLOSE_DIALOG:
    return { ...state, dialogOpen: false };
  default:
    return state;
}

Or are these pretty much equivalent and it's just a matter of preference? The only advantage I can see to using thunks is that one could perform additional processing if the user saved data vs just clicking the close button. It at least leaves that option open for later.

ffxsam
  • 26,428
  • 32
  • 94
  • 144
  • The three action creators in the first example are all equivalent. You're not dispatching USER_CLOSED_DIALOG or USER_SAVED_DATA at all. Is that what you intended? – David L. Walsh Mar 23 '16 at 10:34
  • Just assume that somewhere else (in a React container), `dispatch(userClosedDialog())` or `dispatch(userSavedData())` is being called. – ffxsam Mar 23 '16 at 17:20
  • But are they supposed to dispatch multiple actions? Or are they simply just aliases to `closeDialog()`. If the latter, you don't even need the thunks, you could just write `return closeDialog();`. – David L. Walsh Mar 24 '16 at 00:23
  • They're not necessarily aliases. Logically it works like this: user clicks the 'X' button on the dialog, which triggers `CLOSE_DIALOG`. Or, user clicks "save" which saves some data (async) and then triggers `CLOSE_DIALOG`. – ffxsam Mar 24 '16 at 00:27
  • Did you have a look at this? http://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559 – Nicole Mar 24 '16 at 08:54

0 Answers0