Rematch Select
Selectors plugin for Rematch.
Install
npm install @rematch/select
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: selector state does not refer to the complete state, only the state within the model
Selectors can be called anywhere within your app.
import { select, getState } from '@rematch/core'
select.cart.total(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)
)
}
}