Coder Social home page Coder Social logo

iron-state's Introduction

Iron-state

React state management library based on Hooks and typescript

Installation

yarn add iron-state

Quick Start

create store

import {createAction, createStore, ReducerFn, EffectFn} from 'iron-state'

export enum ReducerActionType {
    increment = "increment",
    decrement = "decrement"
}

export enum EffectActionType {
    asyncIncrement = "asyncIncrement",
    asyncDecrement = "asyncDecrement"
}

interface CounterState {
    count: number
}

type CounterReducer = Record<ReducerActionType, ReducerFn<CounterState>>;

type CounterEffect = Record<EffectActionType, EffectFn>;

const actions = {
    increment: createAction(ReducerActionType.increment)<number>(),
    decrement: createAction(ReducerActionType.decrement)<number>(),
    asyncIncrement: createAction(EffectActionType.asyncIncrement)(),
    asyncDecrement: createAction(EffectActionType.asyncDecrement)(),
};

const { useStore, dispatch } = createStore<CounterState, CounterReducer, CounterEffect>({
    state: {
        count: 0,
    },
    reducers: {
        [ReducerActionType.increment](state, value) {
            state.count = state.count + value
        },
        [ReducerActionType.decrement](state, value) {
            state.count = state.count - value
        },
    },
    effects: {
        async [EffectActionType.asyncIncrement]() {
            await new Promise(resolve => {
                setTimeout(() => {
                    resolve()
                }, 1000)
            });
            await dispatch(actions.increment(1))
        },

        async [EffectActionType.asyncDecrement]() {
            await new Promise(resolve => {
                setTimeout(() => {
                    resolve()
                }, 1000)
            });
            await dispatch(actions.decrement(1))
        },
    },
});

export {
    useStore,
    dispatch,
    actions
}

use store in view

import { useStore, dispatch, actions } from './store/counterStore'
 
const App = () => {
  const count = useStore(s => s.count)

  return (
    <div>
      <span>{count}</span>
      <button onClick={() => dispatch(actions.decrement(1))}>-</button>
      <button onClick={() => dispatch(actions.increment(1))}>+</button>
      <button onClick={() => dispatch(actions.asyncIncrement(1)}>async+</button>
    </div>
  )
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.