2.2.0 • Published 2 years ago

@rematch/core v2.2.0

Weekly downloads
26,609
License
MIT
Repository
github
Last release
2 years ago

Rematch

Rethink Redux.

Rematch is Redux best practices without the boilerplate. No more action types, action creators, switch statements or thunks.

Index

Getting Started

npm install @rematch/core

Step 1: Init

init configures your reducers, devtools & store.

index.js

import { init } from '@rematch/core'
import * as models from './models'

const store = init({
  models,
})

export default store

For a more advanced setup, see plugins and Redux config options.

Step 2: Models

The model brings together state, reducers, async actions & action creators in one place.

models.js

export const count = {
  state: 0, // initial state
  reducers: {
    // handle state changes with pure functions
    increment(state, payload) {
      return state + payload
    }
  },
  effects: {
    // handle state changes with impure functions.
    // use async/await for async actions
    async incrementAsync(payload, rootState) {
      await new Promise(resolve => setTimeout(resolve, 1000))
      this.increment(payload)
    }
  }
}

See the reducers docs to learn more, including how to trigger actions from other models.

Understanding models is as simple as answering a few questions:

  1. What is my initial state? state
  2. How do I change the state? reducers
  3. How do I handle async actions? effects with async/await

Step 3: Dispatch

dispatch is how we trigger reducers & effects in your models. Dispatch standardizes your actions without the need for writing action types or action creators.

import store from './index'

const { dispatch } = store
                                                  // state = { count: 0 }
// reducers
dispatch({ type: 'count/increment', payload: 1 }) // state = { count: 1 }
dispatch.count.increment(1)                       // state = { count: 2 }

// effects
dispatch({ type: 'count/incrementAsync', payload: 1 }) // state = { count: 3 } after delay
dispatch.count.incrementAsync(1)                       // state = { count: 4 } after delay

Dispatch can be called directly, or with the dispatch[model][action](payload) shorthand.

Step 4: View

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider, connect } from 'react-redux'
import store from './index'

const Count = props => (
  <div>
    The count is {props.count}
    <button onClick={props.increment}>increment</button>
    <button onClick={props.incrementAsync}>incrementAsync</button>
  </div>
)

const mapState = state => ({
  count: state.count
})

const mapDispatch = ({ count: { increment, incrementAsync }}) => ({
  increment: () => increment(1),
  incrementAsync: () => incrementAsync(1)
})

const CountContainer = connect(mapState, mapDispatch)(Count)

ReactDOM.render(
  <Provider store={store}>
    <CountContainer />
  </Provider>,
  document.getElementById('root')
)

Migrating From Redux

Moving from Redux to Rematch involves very few steps.

  1. Setup Rematch init with Redux step 1
  2. Mix reducers & models step 2
  3. Shift to models step 3

API

See the @rematch/core API


Like this project? ★ us on Github :)

xdf-ntwmzy-mobile@hackkun/react-store-kitlst_front_mobile_refact@beepsoft/ocho@beepsoft/ocho-2@beepsoft/ocho-3@beepsoft/ocho-6@visualbi/ibcs-charts@everything-registry/sub-chunk-774smart-compose-reactrmkj-routerrematch-immer-combine-persistrematch-modelsto-micro-appsto-micro-app-h5sto-webcallractive-rematchreact-meta-clireact-mothershippri-plugin-rematchrazzle-hacknewsreactor-sandbox-rule-editorreact-template-h5dvaplusdvawebeditrongatsby-source-wordpressgatsby-source-wordpress-experimentalgatsby-plugin-wordpressluban-ssrlayout_antdhm-react-cliinforiver-enterpriseinforiver-standardmicro-app-h5heds_authinternal-libkirkstone-foundation-ui-corek2-modeler-demok2-redux-react-scriptstossjstopfeed-boilerplate-simplets-ui-lib@hardfist/spacra-akcode@hookcompany/rematch-decoratorscra-template-code4-cards@foundry360/store@doraadev/umi-plugin-rematchdecorate-v2db-comp@kkt/plugin-pro-rematchunion-cli-testtwse-clive-h5-postve-pc-react-cascader-areave-pc-react-cascader-postumi-plugin-rematchvav1.1codelight-react-component@johanneswe91/expo-typescript-storybook@jerexyz/umi-plugin-rematch@phmu/gatsby-source-wordpress-experimental@pixel-point/gatsby-source-wordpress@pixel-point/gatsby-source-wordpress-experimental@progital/gatsby-source-wordpress-experimental@nethesis/phone-island@mirrormedia/dual-channel@magecom/gatsby-source-wordpress-experimental-fork-media-items-fix@mdfjs/react@libreforge/libreforge-designer@libreforge/libreforge-framework@mpv-easy/mpv-easy@pd-solucoes/react-user-crud@lensung/miniapp-rematch@lushiyi/dvataro@segment/inspector-core@taylord.tech/material@twreporter/dual-channel@topfeed/topfeed-cli@tntv/charts@tokens-studio/graph-editor@wz-libs/redux-connect@un./hub-react@un./hub-react-taro@uiw-admin/models@verdaccio/ui-components@fast-js/react-uikit@springtype/springtype-incubator-state@springtype/state@aiou/react-template@alicd/cone-scaffold-react-app@extracontrib/breadcrumb-framework@silencerweb/gatsby-source-wordpress-experimental@xrhcc-flow/busiflow@throttleup/gatsby-source-wordpress@acflow/package-example@acflow/react-native-logger-manager@acflow/rnc
2.2.0

2 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-next.10

3 years ago

2.0.0-next.9

3 years ago

2.0.0-next.8

3 years ago

2.0.0-next.7

3 years ago

2.0.0-next.6

4 years ago

2.0.0-next.5

4 years ago

2.0.0-next.4

4 years ago

2.0.0-next.3

4 years ago

2.0.0-next.2

4 years ago

2.0.0-next.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-beta.5

6 years ago

1.0.0-beta.4

6 years ago

1.0.0-beta.3

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

1.0.0-beta.0

6 years ago

1.0.0-alpha.9

6 years ago

1.0.0-alpha.8

6 years ago

1.0.0-alpha.7

6 years ago

1.0.0-alpha.6

6 years ago

1.0.0-alpha.5

6 years ago

1.0.0-alpha.4

6 years ago

1.0.0-alpha.3

6 years ago

1.0.0-alpha.2

6 years ago

1.0.0-alpha.1

6 years ago

1.0.0-alpha.0

6 years ago

0.6.0

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.1.0-beta.8

6 years ago

0.1.0-beta.7

6 years ago

0.1.0-beta.5

6 years ago

0.1.0-beta.4

6 years ago

0.1.0-beta.3

6 years ago

0.1.0-beta.2

6 years ago

0.1.0-beta.1

6 years ago

0.1.0-beta.0

6 years ago

0.1.0-alpha.13

6 years ago

0.1.0-alpha.12

6 years ago

0.1.0-alpha.11

6 years ago

0.1.0-alpha.10

6 years ago

0.1.0-alpha.9

6 years ago

0.1.0-alpha.7

6 years ago

0.0.0-alpha.7

6 years ago

0.1.0-alpha.6

6 years ago

0.1.0-alpha.5

6 years ago

0.1.0-alpha.4

6 years ago

0.1.0-alpha.3

6 years ago

0.1.0-alpha.2

6 years ago

0.1.0-alpha.1

6 years ago