New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@airma/react-state

Package Overview
Dependencies
Maintainers
1
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@airma/react-state

the purpose of this project is make useReducer more simplify

  • 15.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

npm NPM downloads standard

@airma/react-state

@airma/react-state is a simple reducer tool, you can use it to replace useReducer, if you want more functions like state synchronization and asynchronous request manage, you can try use-agent-reducer.

@airma/react-state works like that:

import React from 'react';
import {render} from 'react-dom'
import {useModel} from '@airma/react-state';

function App(){
    const {state, increase, decrease} = useModel((state:number)=>{
        const baseState = state >= 0? state : 0;
        return {
            state: baseState,
            increase(){
                return baseState + 1;
            },
            decrease(){
                return baseState - 1;
            }
        };
    },0);
    return (
        <div>
            <button onClick={increase}>-</button>
            <span>{state}</span>
            <button onClick={decrease}>+</button>
        </div>
    );
}

render(<App/>, document.getElementById('root'));

It calls the model generate function when component is mounting or the model method has been called everytime.

The example about shows how to use API to manage a step counting model with state. We call increase method to generate a next state, then useAir update this state by recall model generator again.

So, the state change flow of increase is like this:

// model function
const model = (state:number)=>{...};

// increase returns
const state = baseState + 1;
// recall model with what increase returns
return model(state);

Yes, it is close with useReducer, but more free for usage. It looks like agent-reducer too, but it support dynamic closure function style, and it is simple enough.

Try not use async methods, @airma/react-state will not support that, the target of @airma/react-state is supporting react local model, but not eating all codes for platform change. We will support transform state from side effect like async request in other ways.

API

useModel

  • model - model generate function, it accepts a state param, and returns a model object, which contains a state, and some methods for generating next state.
  • state - this is the default state for model initialization.
function useModel<S, T extends AirModelInstance<S>>(
    model: AirReducer<S, T>,
    state: ReturnType<typeof model>['state']
): T

Keywords

FAQs

Package last updated on 02 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc