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
model
application states withstatechart
and implement the basic functionalities inxstate
, pay attention to howinvoked
Services 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 onCallback
because it's the most commonly used services in a real world application. -
Read about different kind of Services here
-
Play on [codesandbox]
-
v2
is built uponv1
, but with more delicate handling ofoptimistic update
processing and used different child state to model the app, observe howparallel
states were used to handle different steps of each operation also pay attention to bothhappy
andsorrow
paths. -
Play on [codesandbox]
-
v3
is a slightly differnt version based onv2
using a differentinvoked
Service calledPromise
, pay attention toservices.js
and see howloadItems
anddeleteItem
worked. -
Key different between
Callback
andPromise
service is you get to dispatch events back to the parent just once withPromise
, whereas inCallback
you could usecb
andonReceive
functions to dispatch events multiple times, both has it's own place in an application, hence this example. -
Play on [codesandbox]
-
v4
is 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
Actor
model for communication between child components and their parent. -
Pay attention to how
TodosMachine
spawned childTodoMachine
s 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,
Service
andActor
are 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
states
andevents
are:-
camelCaseForState
-
UPPER.CASE.FOR.EVENT
- By using
dots
for 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
ui
adumb layer
- meaning ui only does two things
- draw the user interface
- accept user inputs (keyboard/mouse events)
- then delegate those events to
xstate
to handle, where all business logics are encapsulated
- meaning ui only does two things
-
Rewrite tests
-
Enable
whyDidYouRender
to eliminate unnecessary renders.