Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 1.55 KB

selectors.md

File metadata and controls

94 lines (70 loc) · 1.55 KB

Selectors

Each store creates a set of default selectors for your convenience.

Get all

Just returns all objects in the collection.

const userStore = createEntityStore('users')

function mapStateToProps(state) {
  return {
    users: userStore.getAll(state)
  }
}

Get specific object

Returns and memoizes single entity object.

const userStore = createEntityStore('users')

function mapStateToProps(state) {
  return {
    user: userStore.getObject(state, 1)
  }
}

Get specific object based on state

Returns and memoizes single entity object.

const userStore = createEntityStore('users')

const getSelectedUser = userStore.createFindSelector(function (state) {
  return state.selectedUserId
})

function mapStateToProps(state) {
  return {
    user: getSelectedUser(state)
  }
}

Given following state, it would return object representing Commander Data.

{
  "entities": {
    "users": [
      {
        "id": 1,
        "name": "LCDR Data"
      }
    ]
  },
  "selectedUserId": 1
}

Get object property

Returns and memoizes single object property

const userStore = createEntityStore('users')

function mapStateToProps(state) {
  return {
    userName: userStore.getProp(state, 1, 'name')
  }
}

Get object flag

Returns and memoizes single object property, assuming it is a boolean

const userStore = createEntityStore('users')

function mapStateToProps(state) {
  return {
    hasName: userStore.getFlag(state, 1, 'name')
    // returns true or false
  }
}