1. model
import { init } from '@rematch/core'
const count = {
state: 0,
reducers: {
upBy: (state, payload) => state + payload
}
}
init({
models: { count }
})
2. View
import { connect } from 'react-redux'
// Component
const mapStateToProps = (state) => ({
count: state.count
})
const mapDispatchToProps = (dispatch) => ({
countUpBy: dispatch.count.upBy
})
connect(mapStateToProps, mapDispatchToProps)(Component)
1. store
import { createStore, combineReducers } from 'redux'
// devtools, reducers, middleware, etc.
export default createStore(reducers, initialState, enhancers)
2. Action Type
export const COUNT_UP_BY = 'COUNT_UP_BY'
3. Action Creator
import { COUNT_UP_BY } from '../types/counter'
export const countUpBy = (value) => ({
type: COUNT_UP_BY,
payload: value,
})
4. Reducer
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
}
}
5. view
import { countUpBy } from '../actions/count'
import { connect } from 'react-redux'
// Component
const mapStateToProps = (state) => ({
count: state.count,
})
connect(mapStateToProps, { countUpBy })(Component)