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 2-6x faster alternative to useState with spread operation.

License

NotificationsYou must be signed in to change notification settings

mutativejs/use-mutative

Repository files navigation

Node CICoverage Statusnpmlicense

A hook to useMutative as a React hook to efficient update react state immutable with mutable way.

use-mutative is 2-6x faster thanuseState() with spread operation, more than 10x faster thanuseImmer().Read more about the performance comparison in Mutative.

Installation

Yarn

yarn add mutative use-mutative

NPM

npm install mutative use-mutative

API

useMutative()

Provide you can create immutable state easily with mutable way.

import{useMutative}from'use-mutative';exportfunctionApp(){const[todos,setTodos]=useMutative([{text:'todo'}]);return(<><buttononClick={()=>{// set todos with draft mutablesetTodos((draft)=>{draft.push({text:'todo 2'});});}}>        click</button><buttononClick={()=>{// also can override value directlysetTodos([{text:'todo'},{text:'todo 2'}]);}}>        click</button></>);}

useMutativeReducer()

Provide you can create immutable state easily with mutable way in reducer way.

For return values that do not contain any drafts, you can userawReturn() to wrap this return value to improve performance. It ensure that the return value is only returned explicitly.

import{rawReturn}from'mutative';import{useMutativeReducer}from'use-mutative';constinitialState={count:0,};functionreducer(draft:Draft<typeofinitialState>,action:{type:'reset'|'increment'|'decrement'}){switch(action.type){case'reset':returnrawReturn(initialState);case'increment':returnvoiddraft.count++;case'decrement':returnvoiddraft.count--;}}exportfunctionApp(){const[state,dispatch]=useMutativeReducer(reducer,initialState);return(<div>      Count:{state.count}<br/><buttononClick={()=>dispatch({type:'increment'})}>Increment</button><buttononClick={()=>dispatch({type:'decrement'})}>Decrement</button><buttononClick={()=>dispatch({type:'reset'})}>Reset</button></div>);}

More detail aboutuse-mutative can be found inAPI docs

Patches

In some cases, you may want to get that patches from your update, we can pass{ enablePatches: true } options inuseMutative() oruseMutativeReducer(), that can provide you the ability to get that patches from pervious action.

const[state,updateState,patches,inversePatches]=useMutative(initState,{enablePatches:true,});const[state,dispatch,patches,inversePatches]=useMutativeReducer(reducer,initState,initializer,{enablePatches:true});

patches format will followhttps://jsonpatch.com/, but the"path" field be array structure.

License

use-mutative isMIT licensed.

About

A 2-6x faster alternative to useState with spread operation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp