0

How gonna assign in const { Types, Creators } in the below code I mean what Types gonna hold and what Creators gonna hold.

   const { Types, Creators } = createActions({
        userRequest: ['username'],
        userSuccess: ['avatar'],
        userFailure: null
    })



var createActions = (function (config, options) {
  if (R.isNil(config)) {
    throw new Error('an object is required to setup types and creators');
  }
  if (R.isEmpty(config)) {
    throw new Error('empty objects are not supported');
  }

  return {
    Types: convertToTypes(config, options),
    Creators: convertToCreators(config, options)
  };
})
bashIt
  • 1,006
  • 1
  • 10
  • 26

2 Answers2

3

The syntax is object destructuring assignment. Types and Creators will be defined as the Types and Creators properties returned from the object returned at createActions() call. For example

const {Types, Creators} = (() => {
  return {Types:0, Creators:1}
})();

console.log(Types, Creators)
guest271314
  • 1
  • 15
  • 104
  • 177
1

This is called a destructuring assignment, It looks at the returned object and assigns the correct key to the variable. Think of it as shorthand for:

const createActions = (function (config, options) {
  if (R.isNil(config)) {
    throw new Error('an object is required to setup types and creators');
  }
  if (R.isEmpty(config)) {
    throw new Error('empty objects are not supported');
  }

  return {
    Types: convertToTypes(config, options),
    Creators: convertToCreators(config, options)
  };
})

let results = createActions({
        userRequest: ['username'],
        userSuccess: ['avatar'],
        userFailure: null
    }),
    Types = results.Types,
    Creators = results.Creators;
D Lowther
  • 1,609
  • 1
  • 9
  • 16