Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋

License

NotificationsYou must be signed in to change notification settings

tnfe/clean-state

Repository files navigation

English |中文

Npm VersionPackage LicenseDownloads

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

npmiclean-state--save

Usage

1.Define a module

// modules/user.tsconststate={name:'test'}constuser={  state,reducers:{setName({payload, state}){return{...state, ...payload}}},effects:{asyncfetchNameAndSet({dispatch}){constname=awaitPromise.resolve('fetch_name')dispatch.user.setName({name})}}}exportdefaultuser;

2.Registration module

// modules/index.tsimportuserfrom'./user'importbootstrapfrom'clean-state'constmodules={ user}exportconst{useModule, dispatch}=bootstrap(modules);

3.Use the module

// page.tsimport{useCallback}from'react'import{useModule,dispatch}from'./modules'functionApp(){/**   * 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')constonChange=useCallback((e)=>{const{ target}=edispatch.user.setName({name:target.value})},[])constonClick=useCallback(()=>{dispatch.user.fetchNameAndSet()},[])return(<divclassName="App"><div><div>          name:{user.name}</div><div>          modify:<inputonChange={onChange}></input></div><buttononClick={onClick}>getUserName</button></div></div>);}exportdefaultApp;

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.tsconstcommon={reducers:{    setValue<State>({payload, state}:{payload:Record<string, any>,state:State}):State{return{...state, ...payload}}}}exportdefaultcommon;// modules/index.tsimportcommontfrom'./common'importuserfrom'./user'import{mixin}from'clean-state';// Mix Common's setValue method into the User moduleconstmodules=mixin(common,{ user})// You can now call the dispatch.user.setValue method on other pagesexportconst{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)

PropertyDescriptionType
modulesA collection of registered modules{string, Module}

useModule(moduleName)

PropertyDescriptionType
moduleNameThe name of the module used returns the corresponding statusstring / string[]

mixin(common, modules)

PropertyDescriptionType
commonPublic modules that need to be injectedModule
modulesA collection of registered modulesModule

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

PropertyDescriptionType
moduleNameThe specific module name of the call should be registered in Bootstrapstring
fnNameThe method name of the call module, reducer/effectstring
payloadThe load value passedobject

Debugging

You can usecs-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

About

🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp