Vue3 Reactivity Extreme
An extreme example of Vue 3 reactivity.
Reactivity Why?
One of the biggest source of headaches in Javascript is the state of variables. When a varible changes, it is often hard to update other parts of code that might be affected by that change. This is particulary challenging when the code is across serveral files. If the code does not react to the changes in varibles there is no reactivity.
Where is my Store?
Usually this reactiviy problem is solved with a “store”. A store is nothing more than code that holds the state of all the variables. When a variable changes, the store updates all its subscribers with this change. Stores can become complex and often involve getters and setters to keep track of changes.
The problem with stores
When your code uses a store to track the variables, if you decide to reuse the code in another project, you will have to bring algong with you the store and the store logic. This for me is no the biggest problem of stores, reusable code.
Vue 3 to the rescue
Vue3 lets you create sniplets of code that keep track of their own reativity and can be used across many different files. These Sniplets are called by convection are called composables, and are pure js. All you need to do is import the reactivity from vue. Since reactivity is a separate module in Vue3, you can actually use it in any project, even if its not vue. In React? we should try