0

Trying to understand a React-Redux codebase which uses a custom middleware in the store. The custom middleware consumes an API Client function which manages making calls to API for the actions passed to it.

In the middleware, there is a variable declaration which I cannot understand. It uses an array as the variable.

const [REQUEST, SUCCESS, FAILURE] = types; 
// types: [REGISTER, REGISTER_SUCCESS, REGISTER_FAIL],

The reducer looks like this:

case LOAD:
  return {
    ...state,
    loading: true
  };
case LOAD_SUCCESS:
  saveAuthCookie( action.result );
  return {
    ...state,
    loading: false,
    loaded: true,
    user: cookie.load( userCookieName ) ? cookie.load( userCookieName ) : action.result
  };
case LOAD_FAIL:
  console.log(`auth LOAD_FAIL with error ${util.inspect(action.error)}`);
  return {
    ...state,
    loading: false,
    loaded: false,
    error: action.error
  };

And the function exported by the reducer that is being called looks like this:

export function load() {
 return {
   types: [LOAD, LOAD_SUCCESS, LOAD_FAIL],
   promise: (client) => client.get( '/auth/loadAuth' )
 };
}
Andrew Stover
  • 301
  • 2
  • 5
  • 5
    `const [REQUEST, SUCCESS, FAILURE] = types` does not initialize an array, it [**destructures**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) an array into 3 `const` variables called `REQUEST`, `SUCCESS`, and `FAILURE`. – mhodges Jan 09 '18 at 17:45
  • 1
    Here `REQUEST === types[0]`, `SUCCESS === types[1]` and so on. – Prakash Sharma Jan 09 '18 at 17:47

0 Answers0