Back to Jotai

Store

docs/core/store.mdx

2.19.1705 B
Original Source

createStore

This function is to create a new empty store. The store can be used to pass in Provider.

The store has three methods: get for getting atom values, set for setting atom values, and sub for subscribing to atom changes.

jsx
const myStore = createStore()

const countAtom = atom(0)
myStore.set(countAtom, 1)
const unsub = myStore.sub(countAtom, () => {
  console.log('countAtom value is changed to', myStore.get(countAtom))
})
// unsub() to unsubscribe

const Root = () => (
  <Provider store={myStore}>
    <App />
  </Provider>
)

getDefaultStore

This function returns a default store that is used in provider-less mode.

js
const defaultStore = getDefaultStore()