Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tim-smart/effect-atom/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The useAtomMount hook mounts an atom in the registry, ensuring it stays alive during the component’s lifecycle without subscribing to its updates. This is useful when you want to keep an atom active but don’t need to read its value.

Signature

function useAtomMount<A>(atom: Atom<A>): void
atom
Atom<A>
required
The atom to mount in the registry

Behavior

  • Mounts the atom when the component renders
  • Keeps the atom mounted while the component is mounted
  • Unmounts the atom when the component unmounts
  • Does not trigger re-renders when the atom value changes

Usage

Keep an atom alive

Use useAtomMount to keep an atom active without reading its value:
import { Atom, useAtomMount } from "@effect-atom/atom-react"
import { Effect } from "effect"

// An atom that performs background work
const backgroundTaskAtom = Atom.make(
  Effect.gen(function* () {
    // Perform some background operation
    yield* Effect.log("Background task running")
    return "completed"
  })
)

function BackgroundWorker() {
  // Keep the atom mounted without reading its value
  useAtomMount(backgroundTaskAtom)
  
  return <div>Background worker active</div>
}

Preload data

Mount atoms to preload data without displaying it:
const userDataAtom = Atom.make(fetchUserData())
const settingsAtom = Atom.make(fetchSettings())

function DataPreloader() {
  // Preload both atoms
  useAtomMount(userDataAtom)
  useAtomMount(settingsAtom)
  
  return null // No UI needed
}

Side-effect atoms

Keep atoms with side effects running:
const analyticsAtom = Atom.make((get) => {
  // Track page views
  const cleanup = trackPageView()
  get.addFinalizer(() => cleanup())
  return "tracking"
})

function AnalyticsProvider({ children }: { children: React.ReactNode }) {
  useAtomMount(analyticsAtom)
  return <>{children}</>
}

Comparison with other hooks

HookSubscribes to updatesReturns valueKeeps mounted
useAtomValueYesYesYes
useAtomMountNoNoYes
useAtomSetNoNoYes

Build docs developers (and LLMs) love