Learning
Before we start
#In the examples we will use the @reatom/framework package which is an umbrella package for the most common reatom packages. You can install it with the command
Basic primitives
#The reatom is based on three basic primitives: Atom, Action and Context. Below you can see how they are used together, after which we will look at each line and what happens in it
Context
#The first thing we did was create a context. It is required for read, modify and subscribe operations. Later we will see what tricks it allows us to do, but now we will focus on the fact that it is enough to create one context for the whole application
Atom
#Create Atom
#An atom is like a variable, it has a type and a value. However, unlike a variable, we can track changes in its value and react to that changes in some way.
To create an atom we use the atom
factory function
In line above we create atom with initial value 1
and name aAtom
.
The name, though not required, will come in handy during the debug stage
Atoms can also be computable, i.e. use the values of other atoms.
This line of code can be read as - “To find out the value of cAtom
you need to read the current values of aAtom
and bAtom
and summarize them”.
Computed atoms should be pure functions to archive the correct order of all computations
Read Atom
#To read the value of an atom you need a previously created context
It is important to note that the retrieval of the value of an atom will happen only after its reading. In other words, it means that if a computed atom has not been read by anyone, the atom will not run the function passed to it
Update Atom
#To change the value in an atom you also need a context, but this time you need to pass it to the atom
The current value of the atom can also be used in the update operation, by passing function
The previous state can also be used in computable atoms
Subscribe to Atom
#Finally you can subscribe to atom changes using context
Actions
#Actions are transactions
#Setting atoms manually is good thing but more often we want to do many changes at once.
Let’s edit example so that we are able to change a
and b
simultaneously.
As we see here the subscribe callback only called with both a and b values changed. This is called “transactions”. They help to reduce the number of subscriber calls, and avoid the creation of unwanted intermediate states
Async actions
#Creating asynchronous actions is also possible, but keep in mind that async operations must be called inside ctx.schedule
callback
Actions nesting
#You can call actions from other actions. And asynchronous actions will return the promise
Advanced
#Multiple contexts
#Contexts are used to glue up atoms and actions, track transactions and many more features. You can use same dependency trees in different contexts:
Computed atoms can’t depend on atom values from different contexts and actions can’t change atoms of different context. Context will initiate new item state referring to that atom.
This enables us to easily test things. But beware of function closures because they are not context dependent!