
Follow up to #2189 ### Change Type - [ ] `patch` — Bug fix - [ ] `minor` — New feature - [ ] `major` — Breaking change - [ ] `dependencies` — Changes to package dependencies[^1] - [x] `documentation` — Changes to the documentation only[^2] - [ ] `tests` — Changes to any test code only[^2] - [ ] `internal` — Any other changes that don't affect the published package[^2] - [ ] I don't know [^1]: publishes a `patch` release, for devDependencies use `internal` [^2]: will not publish a new version
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { useState } from 'react'
|
|
import { Atom, AtomOptions, atom } from '../core'
|
|
|
|
/**
|
|
* Creates a new atom and returns it. The atom will be created only once.
|
|
*
|
|
* See [[atom]]
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const Counter = track(function Counter () {
|
|
* const count = useAtom('count', 0)
|
|
* const increment = useCallback(() => count.set(count.get() + 1), [count])
|
|
* return <button onClick={increment}>{count.get()}</button>
|
|
* })
|
|
* ```
|
|
*
|
|
* @public
|
|
*/
|
|
export function useAtom<Value, Diff = unknown>(
|
|
/**
|
|
* The name of the atom. This does not need to be globally unique. It is used for debugging and performance profiling.
|
|
*/
|
|
name: string,
|
|
/**
|
|
* The initial value of the atom. If this is a function, it will be called to get the initial value.
|
|
*/
|
|
valueOrInitialiser: Value | (() => Value),
|
|
/**
|
|
* Options for the atom.
|
|
*/
|
|
options?: AtomOptions<Value, Diff>
|
|
): Atom<Value, Diff> {
|
|
return useState(() => {
|
|
const initialValue =
|
|
typeof valueOrInitialiser === 'function' ? (valueOrInitialiser as any)() : valueOrInitialiser
|
|
|
|
return atom(`useAtom(${name})`, initialValue, options)
|
|
})[0]
|
|
}
|