Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rematch/select

Package Overview
Dependencies
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rematch/select

Selectors plugin for Rematch

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.8K
increased by6.44%
Maintainers
2
Weekly downloads
 
Created
Source

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: 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.

Keywords

FAQs

Package last updated on 28 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc