React狀態(tài)管理器Rematch的使用詳解
Rematch使用
1. Rematch
介紹
Rematch
是沒有樣板文件的Redux
的最佳實(shí)踐,沒有action types
、 action creators
, 狀態(tài)轉(zhuǎn)換或thunks
。
2. Rematch
特性
Redux
是一個(gè)了不起的狀態(tài)管理工具,由良好的中間件生態(tài)系統(tǒng)和優(yōu)秀的開發(fā)工具支持。Rematch
以 Redux
為基礎(chǔ),減少樣板文件并強(qiáng)制執(zhí)行最佳實(shí)踐。
- 小于 2kb 的大小
- 無需配置
- 減少
Redux
樣板 React
開發(fā)工具支持- 支持動(dòng)態(tài)添加
reducer
Typesctipt
支持- 允許創(chuàng)建多個(gè)
store
- 支持
React Native
- 可通過插件擴(kuò)展
3. 基本使用
以一個(gè)計(jì)數(shù)器(count
)應(yīng)用為例子:
a. 定義模型(Model
) Model
集合了state
、reducers
、async actions
,它是描述Redux store
的一部分以及它是如何變化的,定義一個(gè)模型只需回答三個(gè)小問題:
- 如何初始化`state`? **state** - 如何改變`state`? **reducers** - 如何處理異步`actions`? **effect** with async/await
// ./models/countModel.js export const count = { state: 0, // 初始化狀態(tài) reducers: { // reducers中使用純函數(shù)來處理狀態(tài)的變化 increment(state, payload) { return state = payload }, }, effects: dispatch => ({ // effects中使用非純函數(shù)處理狀態(tài)變化 // 使用async/await處理異步的actions async incrementAsync(payload, rootState) { await new Promise(resolve => setTimeout(resolve, 1000)) dispatch.count.increment(payload) } }) }
// ./models/index.js import { count } from './count' export const models = { count }
b. 初始化store
使用init
方法初始化store
, init
是構(gòu)建配置的Redux
存儲(chǔ)所需的唯一方法。init
的其他參數(shù)可以訪問api了解。
// store.js import { init } from '@rematch/core' import * as models from './models' const store = init({models}) export default store
c. Dispatch actions
可以通過使用dispatch
來改變你的store
中的reducer
和effects
,而不需要使用action types
或 action creators
; Dispatch
可以直接被調(diào)用,也可以使用簡(jiǎn)寫dispatch[model][action](payload)
。
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 }) // 延時(shí)1秒后 state = { count: 3 } dispatch.count.incrementAsync(1) // 延時(shí)1秒后 state = { count: 4 }
d. Rematch
和Redux
一起使用 Rematch
可以和原生Redux
集成一起使用,看下邊這個(gè)例子:
// App.js import React from 'react' import ReactDOM from 'react-dom' import { Provider, connect } from 'react-redux' import store from './store' 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 = (dispatch) => ({ increment: () => dispatch.count.increment(1), incrementAsync: () => dispatch.count.incrementAsync(1), }) const CountContainer = connect( mapState, mapDispatch )(Count) ReactDOM.render( <Provider store={store}> <CountContainer /> </Provider>, document.getElementById('root') )
到此這篇關(guān)于React狀態(tài)管理器Rematch的使用的文章就介紹到這了,更多相關(guān)React狀態(tài)管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React Router 如何使用history跳轉(zhuǎn)的實(shí)現(xiàn)
這篇文章主要介紹了React Router 如何使用history跳轉(zhuǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04reactjs學(xué)習(xí)解決unknown at rule @tailwind css
這篇文章主要介紹了reactjs學(xué)習(xí)解決unknown at rule @tailwind css問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02詳解react-native WebView 返回處理(非回調(diào)方法可解決)
這篇文章主要介紹了詳解react-native WebView 返回處理(非回調(diào)方法可解決),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02React數(shù)據(jù)傳遞之組件內(nèi)部通信的方法
這篇文章主要介紹了React數(shù)據(jù)傳遞之組件內(nèi)部通信的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12React組件中監(jiān)聽函數(shù)獲取不到最新的state問題
這篇文章主要介紹了React組件中監(jiān)聽函數(shù)獲取不到最新的state問題問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01