• 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');
  • 相关阅读:
    Win7操作系统防火墙无法关闭的问题 无法找到防火墙关闭的地方的解决的方法
    【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永只是期
    OC内存管理总结,清晰明了!
    下次自己主动登录(记住password)功能
    linux删除svn版本号库
    Python中可避免读写乱码的一个强慷慨法
    Tomcat源代码阅读#1:classloader初始化
    iOS关于图片点到像素转换之杂谈
    hdu 3804树链剖分+离线操作
    cdn缓存
  • 原文地址:https://www.cnblogs.com/lightsong/p/10867119.html
Copyright © 2020-2023  润新知