Redux counterpart rematch dva
rematch
https://github.com/rematch/rematch#examples
数据模型一个文件定义, 不用分散到 action actiontype reducer 文件
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)
}
})
}
状态定义
import { init } from '@rematch/core'
import * as models from './models'
const store = init({
models,
})
export default store
view使用不变
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 = ({ 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')
)
dva
Lightweight front-end framework based on redux, redux-saga and react-router. (Inspired by elm and choo)
https://github.com/dvajs/dva
import React from 'react';
import dva, { connect } from 'dva';
import './style.css'; // 1. Initialize
const app = dva(); console.log(2); // 2. Model
app.model({
namespace: 'count',
state: 0,
reducers: {
add (count) { return count + 1 },
minus(count) { return count - 1 },
},
}); class TestError extends React.Component {
componentDidCatch(e) {
alert(e.message);
}
componentDidMount() {
// throw new Error('a');
}
render() {
return <div>TestError</div>
}
} // 3. View
const App = connect(({ count }) => ({
count
}))(function(props) {
return (
<div>
<TestError />
<h2>{ props.count }</h2>
<button key="add" onClick={() => { props.dispatch({type: 'count/add'})}}>+</button>
<button key="minus" onClick={() => { props.dispatch({type: 'count/minus'})}}>-</button>
</div>
);
}); // 4. Router
app.router(() => <App />); // 5. Start
app.start('#root');
Redux counterpart rematch dva的更多相关文章
- redux和dva
实习的时候,公司使用的是react,react说实话生态学的还不是很完善,就暂时先不做跟react相关的博客,等以后学好了react全家桶之后,专门再总结一下react的内容 这两天看了公司的alit ...
- React dva 的使用
各大传送门: DvaJS Github DvaJS API与示例 了解 dva- dva = React-Router + Redux + Redux-saga + fetch - dva的初衷是(其 ...
- dva框架使用详解及Demo教程
dva框架的使用详解及Demo教程 在前段时间,我们也学习讲解过Redux框架的基本使用,但是有很多同学在交流群里给我的反馈信息说,redux框架理解上有难度,看了之后还是一脸懵逼不知道如何下手,很多 ...
- redux-saga框架使用详解及Demo教程
redux-saga框架使用详解及Demo教程 前面我们讲解过redux框架和dva框架的基本使用,因为dva框架中effects模块设计到了redux-saga中的知识点,可能有的同学们会用dva框 ...
- JS构建多端应用
JS构建多端应用 一,需求与介绍 1.1,介绍 1,Taro 是一套遵循 React语法规范的 多端开发 解决方案.现如今市面上端的形态多种多样,Web.React-Native.微信小程序等各种端大 ...
- react 前端项目技术选型、开发工具、周边生态
react 前端项目技术选型.开发工具.周边生态 声明:这不是一篇介绍 React 基础知识的文章,需要熟悉 React 相关知识 主架构:react, react-router, redux, re ...
- Taro_Mall 是一款多端开源在线商城小程序.
介绍 Taro_Mall是一款多端开源在线商城应用程序,后台是基于litemall基础上进行开发,前端采用Taro框架编写,现已全部完成小程序和h5移动端,后续会对APP,淘宝,头条,百度小程序进行适 ...
- rematch:当你受不了redux繁琐写法的时候,是时候了解一波rematch了
前言: 前段时间学习完react后,刚好就接到公司一个react项目的迭代,顺便巩固一下前段时间的学习成果.项目使用的是redux+react-router,将所有的数据都放在redux中,异步处理数 ...
- Vuex、Flux、Redux、Redux-saga、Dva、MobX
https://www.jqhtml.com/23003.html 这篇文章试着聊明白这一堆看起来挺复杂的东西.在聊之前,大家要始终记得一句话:一切前端概念,都是纸老虎. 不管是Vue,还是 Reac ...
随机推荐
- 提高python处理数据的效率方法
处理大数据的方法有很多,目前我知道就这么多,后面会持续更新: 一.将数据分批次读取 csv格式是常见的数据存储方式,对于我们普通人而言易于读写.此外,在pandas中有pd.read_csv()函数可 ...
- Linux中怎么升级PHP
推荐yum源安装: #查看 删除老php版本的源 yum list installed | grep php yum remove php.x86_64 php-cli.x86_64 php-comm ...
- wentaolovesmeng.club
wentaolovesmeng.club wentaostudy.club
- SRVCC B1,B2事件总结
何为SRVCC? SRVCC(Single Radio Voice Call Continuity)是3GPP提出的一种VoLTE语音业务连续性方案,主要是为了解决当单射频UE 在LTE网络和2G/3 ...
- java abstractQueue
- MVC 自己创建URL 对象处理路径
var url = new UrlHelper(filterContext.RequestContext); var url = new UrlHelper(HttpContext.Current.R ...
- javascript:location=location;">刷新</a>
<a href="javascript:location=location;">刷新</a>
- Content-Type与MIME
http://www.cnblogs.com/jsean/articles/1610265.html 首先,我们要了解浏览器是如何处理内容的.在浏览器中显示的内容有 HTML.有 XML.有 GIF. ...
- 基于数组的栈(Java)
package com.rao.linkList; /** * @author Srao * @className ArrayStack * @date 2019/12/3 13:41 * @pack ...
- SuperSocket
1.目前稳定版是 v1.6: 2.轻量级.可扩展.Socket应用程序框架: 3.你可以用来开发Socket服务端应用,不用关心如何使用Socket.如何维护Socket连接和Socket如何工作. ...