0.4.4 • Published 9 months ago

react-structured-state v0.4.4

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

React Structured State

About

If we use react state in react-hooks, code tends to be long and compilicated for updating data-structure.

This package make your code simplly for update data-structure state.

This package support following data structure.

  • Array
  • Queue
  • Set
  • Map

Install

$ npm install react-structured-state

Demo App

https://react-structured-state.web.app

Source code of Demo App is here

How to use

Define

with normal(react hooks) state

import { useArray } from 'react-structured-state';

const App = (): JSX.Element => {
  const [arr, actionArr] = useArray<number>([1, 2, 3]);
  return (
    ...
  )
}

with recoil state

import { atom } from 'recoil';
import { useRecoilArray } from 'react-structured-state';

export const arrayState = atom({
  key: 'arrayState',
  default: [1, 2, 3],
});

const App = (): JSX.Element => {
  const [arr, actionArr] = useRecoilArray<number>(arrayState);
  return (
    ...
  )
}

Use Case

push 10 to back

// Before
setArr((oldarr) => [...oldarr, 10]);

// After
actionArr.pushBack(10);

double all elements

// Before
setArr((oldarr) => oldarr.map((e) => e * 2));

// After
actionArr.map((e) => e * 2);

Design of package

useArray/useRecoilArray/useQueue/useRecoilQueue return two values.

First value is used for view data.

Second value is object of action methods used for changing value.

API

In this sction, T mean type of container.

Array

Define action by following.

// with normal state
const [arr, actionArr] = useArray<number>([1, 2, 3]);

// with recoil state
const [arr, actionArr] = useRecoilArray<number>(arrayState);

In this case, methods of actionArr are following.

methodactionargsargs(optional)default
setStatebasic setStateT[]
pushBackadd value(s) on backval: T...vals: T[]none
pushFrontadd value(s) on frontval: T,...vals: T[]none
insertinsert valueidx: number, val: T...vals: T[]none
popBackremove value(s) on backcount: number1
popFrontremove value(s) on frontcount: number1
concatBackadd elements of array on backvals: T[]
concatFrontadd elements of array on frontvals: T[]
sortsort with callbackfn: (vals: T[]) => T[](a, b) => a - b
reversereverse elements
sliceupdate values with slice()start:number, end:number0, length
spliceupdate values with splice()start:numberdeleteCount: number, ...vals: T[]none, none
filterupdate values with filter()fn:(vals: T[]) => boolean
mapupdate values with map()fn:(vals: T[]) => T[]
fillupdate values with fill()val: Tstart: number, end:number0, length
chainupdate vlues with array method chainfn: (vals:T[]) => T[]
setset value on specified indexidx:number, val: T
eraseerase element by specified valueval:T
clearclear all elements

Queue

Actually, Queue implementation in this library is alias of Array methods. So you can use useArray like quque.

But if you use useQueue, you can only use few methods. So you can write code that easy to understand (by expressly Queue).

Define action by following.

// actual type of state is Array

// with normal state
const [que, actionQue] = useQueue<number>([1, 2, 3]);

// with recoil state
const [que, actionQue] = useRecoilQueue<number>(queueState);

In this case, methods of actionQue are following.

methodactionargsargs(optional)default
setStatebasic setStateT[]
pushadd value(s) on backval: T...vals: T[]none
popremove value(s) on frontcount: number1
concatadd elements of array on backvals: T[]
clearclear all elements

Set (HashSet)

// with normal state
const [st, actionSt] = useSet<number>([1, 2, 3]);

// with recoil state
const [st, actionSt] = useRecoilSet<number>(stState);

In this case, methods of actionSt are following.

methodactionargsargs(optional)default
setStatebasic setStateSet
addadd key(s)val: T...vals: T[]none
deletedelete keyval:T
clearclear set

Map (HashMap)

// with normal state
const [mp, actionMp] = useMap<string, number>([['a', 1]]);

// with recoil state
const [mp, actionMp] = useRecoilMap<string, number>(mpState);

In this case, methods of actionSt are following.

TK means type of key, TV means type of value.

methodactionargsargs(optional)default
setStatebasic setStateMap<TK,TV>
setadd or update key/valkey: TK, val: TV
deletedelete key/valkey:TK
clearclear all key/val
0.4.4

9 months ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

3 years ago

0.3.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.1.6

3 years ago

0.2.4

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago