rematch的基本用法
rematch是对redux的二次封装,简化了redux是使用,极大的提高了开发体验。rematch仅仅是对redux的封装,没有依赖redux-saga,也没有关联react,因此其可以用在其他的视图库中,如vue等。
1. rematch的优点
1.省略了action types
不必再多次写字符串,使用model/method代替
2.省略了action creators
直接调用方法,不必再生产action type,使用dispatch.model.method代替
3.省略了switch语句
调用model.method方法,不必判断action type
4.集中书写状态,同步和异步方法
在一个model中使用state,reducers和effects来写状态,同步和异步方法
2. rematch的model
model中直接写state,reducers,effects,十分集中方便
export const count = {
  state: 0, // initial state
  reducers: {
    // handle state changes with pure functions
    increment(state, payload) {
      return state + payload
    }
  },
  effects: (dispatch) => ({
    // handle state changes with impure functions.
    // use async/await for async actions
    async incrementAsync(payload, rootState) {
      await new Promise(resolve => setTimeout(resolve, 1000))
      dispatch.count.increment(payload)
    }
  })
}
3. rematch的dispatch
dispatch可以直接调用同步和异步方法,不必再发送action
import { init } from '@rematch/core'
import * as models from './models'
const store = init({
  models,
})
export 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
4. rematch的状态派发
依然使用redux的connect,mapStateToProps,mapStateToDispatch来派发状态和方法到子组件
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')
)
rematch的基本用法的更多相关文章
- rematch:当你受不了redux繁琐写法的时候,是时候了解一波rematch了
		
前言: 前段时间学习完react后,刚好就接到公司一个react项目的迭代,顺便巩固一下前段时间的学习成果.项目使用的是redux+react-router,将所有的数据都放在redux中,异步处理数 ...
 - Rematch   Redux的替代品
		
前言:Rematch和vuex很像. 文档:https://github.com/yurizhang/rematch 简介: 先看看rematch的官方介绍: Rematch是没有boilerplat ...
 - EditText 基本用法
		
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
 - jquery插件的用法之cookie 插件
		
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
 - Java中的Socket的用法
		
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
 - [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
		
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
 - python enumerate 用法
		
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
 - [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
		
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
 - 【JavaScript】innerHTML、innerText和outerHTML的用法区别
		
用法: <div id="test"> <span style="color:red">test1</span> tes ...
 
随机推荐
- Centos6 Ruby 1.8.7升级至Ruby 2.3.1的方法
			
本文章地址:https://www.cnblogs.com/erbiao/p/9117018.html#现在的版本 [root@hd4 /]# ruby --version ruby (-- patc ...
 - 数据分析处理库Pandas——索引进阶
			
Series结构 筛选数据 指定值 备注:查找出指定数值的索引和数值. 逻辑运算 备注:查找出值大于2的数据. 复合索引 DataFrame结构 显示指定列 筛选显示 备注:值小于0的显示原值,否则显 ...
 - powerpoint教程资料,PPT的
			
Powerpoint,是微软公司设计的演示文稿软件,利用Powerpoint不仅可以创建演示文稿,还可以在互联网上召开面对面会议.远程会议或在网上给观众展示演示文稿,掌握利用PowerPoint是一项 ...
 - QOS-Qos标记和QOS-Policy策略
			
QOS-Qos标记和qos policy策略 2018年7月7日 20:29 主要标记方法 : IP ToS字段标记 IP Precedence(IP优先级) DSCP 二层 802.1p CoS ...
 - HBase 通过myeclipce脚本来获取固定columns(获取列簇中的列及对应的value值)
			
第一步:关联Jar包 1. 配置hadoop-env.sh文件添加Hbase关联jar包 /opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop下编辑hadoop- ...
 - BZ 600题祭
			
不知不觉就600题了呢. 明天就要省选了.不要让这个数字定格在这里吧!
 - C++11中rvalue references的使用
			
Rvalue references are a feature of C++ that was added with the C++11 standard. The syntax of an rval ...
 - 怎么防止别人动态在你程序生成代码(怎么防止别人反编译你的app)
			
1.本地数据加密 iOS应用防反编译加密技术之一:对NSUserDefaults,sqlite存储文件数据加密,保护帐号和关键信息 2.URL编码加密 iOS应用防反编译加密技术之二:对程序中出现的U ...
 - 【廖雪峰老师python教程】——错误和调试
			
错误处理 try...except...finally...机制 try: print('try...') r = 10 / 0 print('result:', r) except ZeroDivi ...
 - 虚拟现实-VR-UE4-编辑自定义Character-上下左右移动-旋转
			
在上一片文章中,我创建了一个自定义的Character,但是只是有一行log显示,我使用了自己的Character,不能有任何操作,这里,我将记录我修改我的Character的过程 万事第一步,打开工 ...