useSticky
Simple and persistent state & fetch hooks built on Zustand and Immer.
Install
Basics
Unlike the useApi
-hook, which comes "pre-made," the useSticky
-hook
is actually created case-by-base in your application with the createSticky()
function, like so:
Now, you can use your sticky hook, like any other hook in React:
Mutations
One of biggest drivers behind this hook is to minimize the boilerplate and make it fast & easy to prototype. As such, the hook combines mutations, selectors and potentially actions in the same definition and usage.
Out of the box, the state comes with a Immer-ready mutate-function, that used directly everywhere you use the hook:
In the example above, the mutate()
is basically a wrapper around the produce()
function in Immer.
Define mutations beforehand
Sometimes it can more practical to define the mutations alongside the hook creation. This is especially usfull in more complex mutations, where you want to include them in the tests for the hook.
Her are the same example as before, but with the setName()
mutation predefined:
Notis that the sate
now is defined as a function that returns the state.
This is to make available the mutate()
function for the predefined mutator setName()
.
Persistence & Storage
If nothing else is defined, the persistens of the hook is basically the same as a Redux state. It will persevered in memory in the SPA as long as the page is not reloaded.
To persist the states between hard page loads, you can provide a storage
& storageId
.
Typically, the storage is either sessionStorage
or localStorage
, but it can be any
synchronous storage with the same API.
In the following example, we are using sessionStorage
to persist the state:
Now, every time changes to the state are made, the storage will be updated.
You can also define expireAfter
if you would like to discard the stored
value at some point (this will only be checked when the page loads in).
Selectors
Since the sticky state is built on Zustand, you can also use a selector directly in the hook. This is useful if you have a larger state, and you don't want React to re-render the component for every change in the state.
Now, React will only re-render the length of the coupon-list changes. Neat!