1.1.2 • Published 9 months ago

@rctool/reactx v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

store

一个轻量、简单的React 状态管理库

Example

import React from 'react';
import ReactDOM from 'react-dom';
import { Store } from '@rctool/store';

interface CounterIn{
  counter: number;
}

type StoreMutationKeys = 'increment' | 'decrement'

type StoreActionKeys = 'asyncIncrement' | 'asyncDecrement'

const delay = (time) => new Promise((res) => {
  setTimeout(() => {
    res(true);
  }, time);
});

const { StoreProvide, useStore } = new Store<CounterIn, StoreMutationKeys, StoreActionKeys>({
  state: {
    counter: 0,
  },
  mutations: {
    increment: ( state, num) => {
      state.counter += 1;
    },
    decrement: ( state, num) => {
      state.counter -= 1;
    },
  },
  actions: {
    async asyncIncrement(commit, num) {
      await delay(1000);
      commit.increment(num);
    },
    async asyncDecrement(commit, num) {
      await delay(1000);
      commit.decrement(num);
    },
  },
});

const Counter = () => {
  const { state, commit, dispatch } = useStore();
  return (
    <>
      <button onClick={() => {
        commit.increment(1);
      }}>
       increment
      </button>
      <button onClick={() => {
        commit.decrement(1);
      }}>
        decrement
      </button>
      <button onClick={() => {
        dispatch.asyncDecrement(1);
      }}>
      asyncDecrement
      </button>
      <button onClick={() => {
        dispatch.asyncIncrement(1);
      }}>
       asyncIncrement
      </button>
      <div>{state.counter}</div>
    </>
  );
};


ReactDOM.render(
  <React.StrictMode>
    <StoreProvide>
      <Counter />
    </StoreProvide>
  </React.StrictMode>
  ,
  document.getElementById('root'),
);
1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

10 months ago