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');