3.0.7 • Published 4 years ago

redumd v3.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Redumd

Redumd 是基于 redux 封装的易于使用的状态管理框架。

  • 将 redux 的 action,reduce 和 selector 等以 Model 的形式放在一个模块内实现(类似与 Dva.js 的 model);
  • 集成 redux-saga,轻松实现 redux 的异步逻辑;
  • 内置易于使用的业务状态和应用状态管理 Model。

用法

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { init, Model } from 'redumd';

class Counter extends Model {
  static defaultState = {
    timers: 0
  };

  static reducers = {
    increment: function(state) {
      return { timers: state.timers + 1 };
    },
    decrement: function(state) {
      return { timers: state.timers - 1 };
    }
  };

  constructor(namespace) {
    super(namespace);

    this.getTimers = state => this.getState(state).timers;
  }
}

class Demo extends Component {
  incrementIfOdd = () => {
    if (this.props.value % 2 !== 0) {
      this.props.onIncrement();
    }
  };

  incrementAsync = () => {
    setTimeout(this.props.onIncrement, 1000);
  };

  render() {
    const { value, onIncrement, onDecrement } = this.props;
    return (
      <p>
        Clicked: {value} times <button onClick={onIncrement}>+</button>{' '}
        <button onClick={onDecrement}>-</button>{' '}
        <button onClick={this.incrementIfOdd}>Increment if odd</button>{' '}
        <button onClick={this.incrementAsync}>Increment async</button>
      </p>
    );
  }
}

const counter1 = new Counter('counter1');
const counter2 = new Counter('counter2');
const store = init({
  models: [counter1, counter2]
});
const rootEl = document.getElementById('root');

const render = () =>
  ReactDOM.render(
    <div>
      <div>
        <h1>Counter1</h1>
        <Demo
          value={counter1.getTimers(store.getState())}
          onIncrement={() => store.dispatch(counter1.actions.increment())}
          onDecrement={() => store.dispatch(counter1.actions.decrement())}
        />
      </div>
      <div>
        <h1>Counter2</h1>
        <Demo
          value={counter2.getTimers(store.getState())}
          onIncrement={() => store.dispatch(counter2.actions.increment())}
          onDecrement={() => store.dispatch(counter2.actions.decrement())}
        />
      </div>
    </div>,
    rootEl
  );

render();
store.subscribe(render);

文档

示例

License

MIT

3.0.7

4 years ago

3.0.6

4 years ago

3.0.4

4 years ago

3.0.5

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago