All Projects β†’ tnfe β†’ Clean State

tnfe / Clean State

Licence: mit
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. πŸ‹

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Clean State

Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+87.85%)
Mutual labels:  hooks, flux, state
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-70.09%)
Mutual labels:  flux, mobx, state
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (-39.25%)
Mutual labels:  flux, state, reducer
Use Substate
πŸ™ Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (-9.35%)
Mutual labels:  hooks, reducer
Outstated
Simple hooks-based state management for React
Stars: ✭ 102 (-4.67%)
Mutual labels:  hooks, state
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+2630.84%)
Mutual labels:  state, reducer
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (-27.1%)
Mutual labels:  hooks, mobx
Useeffectreducer
useReducer + useEffect = useEffectReducer
Stars: ✭ 642 (+500%)
Mutual labels:  state, reducer
Mobx Router
A simple router for MobX + React apps
Stars: ✭ 489 (+357.01%)
Mutual labels:  hooks, mobx
Little State Machine
πŸ“  React custom hook for persist state management
Stars: ✭ 654 (+511.21%)
Mutual labels:  flux, state
Tng Hooks
Provides React-inspired 'hooks' like useState(..) for stand-alone functions
Stars: ✭ 954 (+791.59%)
Mutual labels:  hooks, state
React Recomponent
πŸ₯« Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (+154.21%)
Mutual labels:  state, reducer
react-pits
React δΈ­ηš„ε‘
Stars: ✭ 29 (-72.9%)
Mutual labels:  flux, mobx
Almin
Client-side DDD/CQRS for JavaScript.
Stars: ✭ 477 (+345.79%)
Mutual labels:  flux, state
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (-49.53%)
Mutual labels:  hooks, state
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+429.91%)
Mutual labels:  flux, state
Simple Todo With React And
πŸ“ a simple react demo to learn flux/reflux/redux
Stars: ✭ 29 (-72.9%)
Mutual labels:  flux, mobx
Use Global Context
A new way to use β€œuseContext” better
Stars: ✭ 34 (-68.22%)
Mutual labels:  hooks, state
Juicr.js
A simple (and tiny <1kb) redux inspired reducer for handling state changes.
Stars: ✭ 102 (-4.67%)
Mutual labels:  flux, reducer
eventrix
Open-source, Predictable, Scaling JavaScript library for state managing and centralizing application global state. State manage system for react apps.
Stars: ✭ 35 (-67.29%)
Mutual labels:  hooks, state

English | δΈ­ζ–‡

Npm Version Package License Downloads

Overview

🐻 Clean-State is a neat, compact state management tool. It drops all of React's historical baggage, uses native hooks to implement it, and gets rid of Redux's problem of invalid rendering during status updates. At the architectural level it is automatically organized through a very simple API. πŸ‹ If you're not building an aircraft carrier and you're tired of having a large, complex and unwield-of-use State management library, try clean-state. It is small and exquisite, the performance of the extreme can meet your needs.

Features

  1. Using native hooks implementation, zero external dependencies.
  2. The structure is simple, the module layer granularity is fine and measurable, and the division is clear.
  3. Excellent performance, can do module level accurate update.
  4. Native support side effects.
  5. It's extremely small, just 200 lines of code.
  6. Just React syntax, zero learning access cost.
  7. TypeScript friendly and automatically deduces module types.
  8. Support for Redux - Tool debugging tool.
  9. Perfect support for RN.

Installation

npm i clean-state --save

Usage

1.Define a module

// modules/user.ts
const state = {
  name: 'test'
}

const user = {
  state,
  reducers: {
    setName({payload, state}) {
      return {...state, ...payload}
    }
  },
  effects: {
    async fetchNameAndSet({dispatch}) {
      const name = await Promise.resolve('fetch_name')
      dispatch.user.setName({name})
    }
  }
}

export default user;

2.Registration module

// modules/index.ts
import user from './user'
import bootstrap from 'clean-state'

const modules = { user }
export const {useModule, dispatch}  = bootstrap(modules);

3.Use the module

// page.ts
import {useCallback} from 'react'
import { useModule, dispatch } from './modules'

function App() {
  /** 
   * Here you can also pass in an array and return multiple module states at the same time 
   * const {user, project} = useModule(['user', 'project'])
   */
  const { user } = useModule('user')
  const onChange = useCallback((e)=> {
    const { target } = e
    dispatch.user.setName({name: target.value})
  }, [])

  const onClick = useCallback(()=> {
    dispatch.user.fetchNameAndSet()
  }, [])

  return (
    <div className="App">
      <div>
        <div>
          name: {user.name}
        </div>
        <div>
          modify: <input onChange={onChange}></input>
        </div>
        <button onClick={onClick}>getUserName</button>
      </div>
    </div>
  );
}

export default App;

Mixin

In many cases, there are common states, reducers, or effects between multiple modules, and here we expose the methods to prevent users from making duplicate declarations in each module.

// common.ts
const common = {
  reducers: {
    setValue<State>({payload, state}: {payload: Record<string, any>, state: State}): State {
      return {...state, ...payload}
    }
  }
}
export default common;

// modules/index.ts
import commont from './common'
import user from './user'
import { mixin } from 'clean-state';

// Mix Common's setValue method into the User module
const modules = mixin(common, { user })

// You can now call the dispatch.user.setValue method on other pages
export const {useModule, dispatch}  = bootstrap(modules);

Module

state

Module state, which is a property object.

{
    name: 'zhangsan',
    order: 1
}

reducers

A collection of handlers that change the state of a module, returning the latest state.

{
    setValue({payload, state, rootState}) {
        return {...state, ...payload}
    }
}

effects

Module's collection of side effects methods that handle asynchronous calls.

{
    async fetchAndSetValue({payload, state, rootState, dispatch}) {
        const newOrder = await fetch('xxx')
        dispatch.user.setValue({order: newOrder})
    }
}

API

bootstrap(modules)

Property Description Type
modules A collection of registered modules {string, Module}

useModule(moduleName)

Property Description Type
moduleName The name of the module used returns the corresponding status string / string[]

mixin(common, modules)

Property Description Type
common Public modules that need to be injected Module
modules A collection of registered modules Module

dispatch.{moduleName}.{fnName}(payload)

Property Description Type
moduleName The specific module name of the call should be registered in Bootstrap string
fnName The method name of the call module, reducer/effect string
payload The load value passed object

Debugging

You can use cs-redux-devtool to debug your project and track historical data changes.

Notice

Dispatch calls take precedence at effects-> reducers, so when there are reducers and effects with the same name under a module, only effects are executed.

Issues

If you have better suggestions for this library, or have any problems using it, you can write them here: https://github.com/tnfe/clean-state/issues

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].