Fetching data from a remote API or database is a core task for most applications.
Cerberus Signals provide foundational tools like the createQuery
primitive and createMutation to manage
asynchronous data.
Fetching data with signals will automatically cache the response until it is invalidated through a mutation.
To fetch (and cache) data using signals, simply follow two steps:
useQuery hook in your componentIn this example there are a few things happening:
currentUser stateWhen you pass an Accessor into the query definition, it will auto-fetch, invalidate, and cache the result when the signal Accessor updates. This means, with this design mutations are not neccessary.
Sometimes, you may have a complex scenario where that prevents you from using a signal utilized as the query "data store". This is where mutations come in.
In order to mutate non-signal based data, follow these steps:
Here's what's happening in this demo:
mutate action to trigger the mutation callback and invalidation{
"id": "411809ca-e360-4f6f-aa8f-2b0e574dac8b",
"name": "User 411809ca-e360-4f6f-aa8f-2b0e574dac8b"
}{
"id": "",
"name": ""
}