Skip to content

0.3.0

Compare
Choose a tag to compare
@aralroca aralroca released this 30 Sep 18:45
· 86 commits to master since this release
a820cb2

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)