0.3.0
This introduces callbacks, that are executed for any property change. Is useful for example to fetch data to an endpoint after the state change, and roll back the state if the request fails.
const initialState = {
quantity: 2
}
// This is new
const callbacks = {
quantity: (newValue, prevValue, setValue) => {
// Update quantity from API
fetch('/api/quantity', { method: 'POST', body: newValue })
// Revert state change if it fails
.catch(e => setValue(prevValue))
}
}
const { Provider, useQuantity } = createStore(initialState, callbacks)