3.1.2 • Published 2 years ago

@rematch/select v3.1.2

Weekly downloads
7,092
License
MIT
Repository
github
Last release
2 years ago

Rematch Select

Selectors plugin for Rematch.

Install

npm install @rematch/select

For @rematch/core@0.x use @rematch/select@0.3.0

Setup

import selectorsPlugin from '@rematch/select'
import { init } from '@rematch/core'

const select = selectorsPlugin()

init({
  plugins: [select]
})

selectors

selectors: { [string]: (state, ...params) => any }

Selectors are read-only snippets of state.

{
  name: 'cart',
  state: [{
    price: 42.00,
    amount: 3,
  }],
  selectors: {
    total(state) {
      return state.reduce((a, b) => a + (b.price * b.amount), 0)
    }
  }
}

note: By default, the selector state does not refer to the complete state, only the state within the model. To change this behavior, use the sliceState configuration option described below.

Selectors can be called anywhere within your app.

import { select } from '@rematch/select'

const store = init({ ... })

select.cart.total(store.getState())

Selectors can also be used with memoization libraries like reselect.

import { createSelector } from 'reselect'

{
  selectors: {
    total: createSelector(
      state => state.reduce((a, b) => a + (b.price * b.amount), 0)
    )
  }
}

Configuration Options

The selectorPlugin() method will accept a configuration object with the following property.

sliceState:

sliceState: (rootState, model) => any

An option that allows the user to specify how the state will be sliced before being passed to the selectors. The function takes the rootState as the first parameter and the model corresponding to the selector as the second parameter. It should return the desired state slice required by the selector.

The default is to return the slice of the state that corresponds to the owning model's name, but this assumes the store is a Javascript object. Most of the time the default should be used. However, there are some cases where one may want to specify the sliceState function.

Example 1 - Use the root state in selectors as opposed to a slice:

This can easily be accomplished by returning the rootState in the getState config:

const select = selectorsPlugin({ sliceState: rootState => rootState });

Now the state parameter that is passed to all of the selectors will be the root state.

Example 2 - Use an Immutable JS object as the store

If you are using an Immutable.js Map as your store, you will need to slice the state using Map.get():

const select = selectorsPlugin({ sliceState: (rootState, model) => rootState.get(model.name) })

Now you can use an Immutable.js Map as your store and access the appropriate slice of the state in each of your selectors.

3.1.2

2 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-next.10

3 years ago

3.0.0-next.9

3 years ago

3.0.0-next.8

3 years ago

3.0.0-next.7

3 years ago

3.0.0-next.6

4 years ago

3.0.0-next.5

4 years ago

3.0.0-next.4

4 years ago

3.0.0-next.3

4 years ago

3.0.0-next.2

4 years ago

3.0.0-next.1

4 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-alpha.0

6 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago