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 faster and more flexible utilities for using Mutative with XState

License

NotificationsYou must be signed in to change notification settings

mutativejs/xstate-mutative

Repository files navigation

Node CInpmlicense

A faster and more flexible utilities for usingMutative with XState

xstate-mutative is more than 10x faster than@xstate/immer.Read more about the performance comparison in Mutative.

Installation

In order to use the Mutative utilities in XState, you will need to install Mutative and XState as a direct dependency.

npm install xstate mutative xstate-mutative# Or use any package manager of your choice.

Usage

Import the Mutative utilities:

import{createMachine,interpret}from'xstate';import{assign,createUpdater}from'xstate-mutative';constlevelUpdater=createUpdater('UPDATE_LEVEL',(ctx,{ input})=>{ctx.level=input;});consttoggleMachine=createMachine({id:'toggle',context:{count:0,level:0,},initial:'inactive',states:{inactive:{on:{TOGGLE:{target:'active',// Immutably update context the same "mutable"// way as you would do with Mutative!actions:assign((ctx)=>ctx.count++),},},},active:{on:{TOGGLE:{target:'inactive',},// Use the updater for more convenience:[levelUpdater.type]:{actions:levelUpdater.action,},},},},});consttoggleService=interpret(toggleMachine).onTransition((state)=>{console.log(state.context);}).start();toggleService.send({type:'TOGGLE'});// { count: 1, level: 0 }toggleService.send(levelUpdater.update(9));// { count: 1, level: 9 }toggleService.send({type:'TOGGLE'});// { count: 2, level: 9 }toggleService.send(levelUpdater.update(-100));// Notice how the level is not updated in 'inactive' state:// { count: 2, level: 9 }

Mutative Options

Credits

xstate-mutative is inspired by@xstate/immer.

It uses the same API as@xstate/immer but uses Mutative under the hood. The repository is based on the@xstate/immer repository.

License

xstate-mutative isMIT licensed.

About

A faster and more flexible utilities for using Mutative with XState

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp