createAtom

Signature
createAtom<T>(atom: Atom<T>): [Accessor<T>, SetterFn<T>]

valdres-solid Subscribe to and update an atom's value

Returns a Solid accessor and a setter function for an atom. The accessor is reactive — any createEffect or component render that reads it will re-run when the atom changes.

Usage

import { atom } from "valdres"
import { createAtom } from "valdres-solid"

const countAtom = atom(0)

function Counter() {
    const [count, setCount] = createAtom(countAtom)
    return (
        <div>
            <span>{count()}</span>
            <button onClick={() => setCount(c => c + 1)}>+</button>
        </div>
    )
}

Returns

IndexTypeDescription
[0]Accessor<T>Reactive accessor — call it to read the current value
[1]SetterFn<T>Function to update the atom's value

Updater function

The setter supports both direct values and updater functions:

setCount(42)              // Set directly
setCount(prev => prev + 1) // Update based on previous value

Note
If you only need to read the value, use createValue instead. If you only need the setter, use createSetAtom to avoid subscribing to changes.

See also