useStore

Signature
useStore(): Store

valdres-react Access the current store instance

Returns the current valdres store from the nearest <Provider>, or the default global store if no Provider is present. Useful when you need direct store access for imperative operations.

Usage

import { useStore } from "valdres-react"

function Debug() {
    const store = useStore()

    const handleClick = () => {
        // Direct store access for imperative operations
        const value = store.get(myAtom)
        console.log("Current value:", value)
    }

    return <button onClick={handleClick}>Log value</button>
}

Transactions

Access the store to perform batched updates:

function BatchUpdate() {
    const store = useStore()

    const handleReset = () => {
        store.txn(set => {
            set(nameAtom, "")
            set(emailAtom, "")
            set(ageAtom, 0)
        })
    }

    return <button onClick={handleReset}>Reset all</button>
}

See also

  • Provider — provide a store to a React subtree
  • store — create a store instance