網站首頁 編程語言 正文
Rematch使用
1. Rematch
介紹
Rematch
是沒有樣板文件的Redux
的最佳實踐,沒有action types
、 action creators
, 狀態轉換或thunks
。
2. Rematch
特性
Redux
是一個了不起的狀態管理工具,由良好的中間件生態系統和優秀的開發工具支持。Rematch
以 Redux
為基礎,減少樣板文件并強制執行最佳實踐。
- 小于 2kb 的大小
- 無需配置
- 減少
Redux
樣板 -
React
開發工具支持 - 支持動態添加
reducer
-
Typesctipt
支持 - 允許創建多個
store
- 支持
React Native
- 可通過插件擴展
3. 基本使用
以一個計數器(count
)應用為例子:
a. 定義模型(Model
) Model
集合了state
、reducers
、async actions
,它是描述Redux store
的一部分以及它是如何變化的,定義一個模型只需回答三個小問題:
- 如何初始化`state`? **state** - 如何改變`state`? **reducers** - 如何處理異步`actions`? **effect** with async/await
// ./models/countModel.js export const count = { state: 0, // 初始化狀態 reducers: { // reducers中使用純函數來處理狀態的變化 increment(state, payload) { return state = payload }, }, effects: dispatch => ({ // effects中使用非純函數處理狀態變化 // 使用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
是構建配置的Redux
存儲所需的唯一方法。init
的其他參數可以訪問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
可以直接被調用,也可以使用簡寫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 }) // 延時1秒后 state = { count: 3 } dispatch.count.incrementAsync(1) // 延時1秒后 state = { count: 4 }
d. Rematch
和Redux
一起使用 Rematch
可以和原生Redux
集成一起使用,看下邊這個例子:
// 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') )
原文鏈接:https://juejin.cn/post/7140846955088838664
相關推薦
- 2022-05-28 Pyinstaller打包Pytorch框架所遇到的問題_python
- 2022-07-11 UVM中uvm_config_db非直線的設置與獲取
- 2022-05-29 一起來看看C語言的預處理注意點_C 語言
- 2022-02-28 Uncaught Error: Module parse failed: Unexpected to
- 2022-08-28 ERROR 1366 (HY000): Incorrect string value: ‘\xE8\
- 2022-04-24 如何在Python中隱藏和加密密碼示例詳解_python
- 2022-11-23 詳解Android創建Handler的必備知識點_Android
- 2022-04-07 Qt實現邊加載數據邊顯示頁面的示例代碼_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支