Redux is an amazing state management tool, supported by a healthy middleware ecosystem and excellent devtools.
Rematch builds upon Redux by reducing boilerplate and enforcing best practices.
To clarify, Rematch removes the need for:
- declared action types
- action creators
- thunks
- store configuration
- mapDispatchToProps
- sagas
A comparison of Rematch & Redux may help clear things up.
import { init } from '@rematch/core'
const count = {
state: 0,
reducers: {
upBy: (state, payload) => state + payload
}
}
init({
models: { count }
})
import { connect } from 'react-redux'
// Component
const mapStateToProps = (state) => ({
count: state.count
})
const mapDispatchToProps = (dispatch) => ({
countUpBy: dispatch.count.upBy
})
connect(mapStateToProps, mapDispatchToProps)(Component)
import { createStore, combineReducers } from 'redux'
// devtools, reducers, middleware, etc.
export default createStore(reducers, initialState, enhancers)
export const COUNT_UP_BY = 'COUNT_UP_BY'
import { COUNT_UP_BY } from '../types/counter'
export const countUpBy = (value) => ({
type: COUNT_UP_BY,
payload: value,
})
import { COUNT_UP_BY } from '../types/counter'
const initialState = 0
export default (state = initialState, action) => {
switch (action.type) {
case COUNT_UP_BY:
return state + action.payload
default: return state
}
}
import { countUpBy } from '../actions/count'
import { connect } from 'react-redux'
// Component
const mapStateToProps = (state) => ({
count: state.count,
})
connect(mapStateToProps, { countUpBy })(Component)
Redux | Rematch | |
---|---|---|
simple setup | ✔ | |
less boilerplate | ✔ | |
readability | ✔ | |
configurable | ✔ | ✔ |
redux devtools | ✔ | ✔ |
generated action creators | | ✔ |
global dispatch | | ✔ |
async | thunks | async/await |