zoukankan      html  css  js  c++  java
  • 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');
  • 相关阅读:
    ⑦linux pidstat
    ⑥linux mpstat
    ⑤linux 系统负载
    ④linux 进程优先级
    ③linux 进程管理
    ②linux 监控进程状态
    ①linux 进程概述于生命周期
    ④linux 自动挂载
    ③linux Gdisk
    ②linux fdisk
  • 原文地址:https://www.cnblogs.com/lightsong/p/10867119.html
Copyright © 2011-2022 走看看