Skip to content

Latest commit

History

History
94 lines (67 loc) 路 2.59 KB

immer.md

File metadata and controls

94 lines (67 loc) 路 2.59 KB

This doc describes jotai/immer bundle.

Install

You have to install immer to access this bundle and its functions.

npm install immer
# or
yarn add immer

atomWithImmer

atomWithImmer creates a new atom similar to the regular atom with a different writeFunction. In this bundle, we don't have read-only atoms, because the point of these functions is the immer produce(mutability) function. The signature of writeFunction is (get, set, update: (draft: Draft<Value>) => void) => void.

import { useAtom } from 'jotai'
import { atomWithImmer } from 'jotai/immer'

const countAtom = atomWithImmer(0)

const Counter = () => {
  const [count] = useAtom(countAtom)
  return <div>count: {count}</div>
}

const Controls = () => {
  const [, setCount] = useAtom(setCountAtom)
  // setCount === update : (draft: Draft<Value>) => void
  const inc = () => setCount((c) => (c = c + 1))
  return <button onClick={inc}>+1</button>
}

Examples

Check examples with atomWithImmer:

withImmer

withImmer takes an atom and returns a derived atom, same as atomWithImmer it has a different writeFunction.

import { useAtom, atom } from 'jotai'
import { withImmer } from 'jotai/immer'

const primitiveAtom = atom(0)
const countAtom = withImmer(primitiveAtom)

const Counter = () => {
  const [count] = useAtom(countAtom)
  return <div>count: {count}</div>
}

const Controls = () => {
  const [, setCount] = useAtom(setCountAtom)
  // setCount === update : (draft: Draft<Value>) => void
  const inc = () => setCount((c) => (c = c + 1))
  return <button onClick={inc}>+1</button>
}

useImmerAtom

This hook takes an atom and replaces the atom's writeFunction with the new immer-like writeFunction like the previous helpers.

import { useAtom } from 'jotai'
import { useImmerAtom } from 'jotai/immer'

const primitiveAtom = atom(0)

const Counter = () => {
  const [count] = useImmerAtom(primitiveAtom)
  return <div>count: {count}</div>
}

const Controls = () => {
  const [, setCount] = useImmerAtom(primitiveAtom)
  // setCount === update : (draft: Draft<Value>) => void
  const inc = () => setCount((c) => (c = c + 1))
  return <button onClick={inc}>+1</button>
}

It would be better if you don't use withImmer and atomWithImmer with useImmerAtom because they provide the immer-like writeFunction and we don't need to create a new one.

Codesandbox

A good example of this bundle in codesandbox.