Each one was built upon the previous one, hence it's recommended to start with crud-v1-services and move forward from there. All examples also available on codesandbox, feel free to play and fork those examples, and report any issues you found here, PRs welcome too.
All these examples are built with StatesKit - a visual statechart editor where you can drag and drop to creat states, events and stuff, no need to code at all, the goal is to help dumping the ideas from your brain to states real quick, don't forget to give it shot! 😎
Also here's a detailed write up on statecharts introducing it's core benefits.
-
A typical CRUD app showing how to
modelapplication states withstatechartand implement the basic functionalities inxstate, pay attention to howinvokedServices are used to serve different API calls. -
There are four kind of services in
xstate, which arePromise, Callback, Observable and Machine, for this example we are focused onCallbackbecause it's the most commonly used services in a real world application. -
Read about different kind of Services here
-
Play on [codesandbox]
-
v2is built uponv1, but with more delicate handling ofoptimistic updateprocessing and used different child state to model the app, observe howparallelstates were used to handle different steps of each operation also pay attention to bothhappyandsorrowpaths. -
Play on [codesandbox]
-
v3is a slightly differnt version based onv2using a differentinvokedService calledPromise, pay attention toservices.jsand see howloadItemsanddeleteItemworked. -
Key different between
CallbackandPromiseservice is you get to dispatch events back to the parent just once withPromise, whereas inCallbackyou could usecbandonReceivefunctions to dispatch events multiple times, both has it's own place in an application, hence this example. -
Play on [codesandbox]
-
v4is based on David'sTodoMVC example but with a couple of improvements. -
This is by far the most complicated example, it showcased how to use the latest
Actormodel for communication between child components and their parent. -
Pay attention to how
TodosMachinespawned childTodoMachines and pass it's ref to each child component as a local single truth that handles the component state., more details in the folder'sReadme.md -
See detailed docs on actor here, this is something you don't want to miss 😎
-
In short,
ServiceandActorare basically the same thing but used differently, rule of thumb:- Statically invoke services (you have to write all services in machine statemenet in advance)
- Dynamically spawn actors (you can spawn new actors from any events whenever needed)
-
Play on [codesandbox]
-
Generic naming convention for
statesandeventsare:-
camelCaseForState -
UPPER.CASE.FOR.EVENT- By using
dotsfor event it is possible in the future to implement wildcase event matching, for exampleUPPER.*to match all events starting withUPPER
- By using
-
-
Basic guiding rule for all these example are hoping to make
uiadumb layer- meaning ui only does two things
- draw the user interface
- accept user inputs (keyboard/mouse events)
- then delegate those events to
xstateto handle, where all business logics are encapsulated
- meaning ui only does two things
-
Rewrite tests
-
Enable
whyDidYouRenderto eliminate unnecessary renders.