Using XState for the FavoritedImages Surface
Learn about incorporating and integrating XState machines for tasks and implementing a basic state machine.
We'll cover the following...
So far, we have set up a basic machine that could be used to control the user flow in the app. Let’s add a new machine for our real-world use case: liking images in a social media clone app.
Minimum viable product
We’ll start by creating a machine minimum viable product (MVP):
Let’s analyze this code from the top:
Line 3: We start by importing the
createMachinefunction, which we use on the very first line of thelikeImagesMachinefunction on line 9.Lines 13–18: We set the ID of the machine and the context of the machine. Bear in mind that XState context is different from React context. We’ve talked a lot about ReactJS context; we know it can be used to share state between components. XState context is a container for quantitative data (such as strings, arrays, or objects), which can potentially be infinite. The array of liked images is a great example of this sort of data, and that’s why we’ll be ...