Skip to content

useResolveOptional

useResolveOptional is useResolve with a miss as an answer rather than an error.

Signature

ts
useResolveOptional<T>(token: InjectionToken<T>, mode: ResolveMode = "nearest"): T | undefined
tsx
import { useResolveOptional } from "@remodulo/react"

function OrderExportBar() {
    // `undefined` when nothing at or above this module registered the token.
    const exporter = useResolveOptional(OrderExporter)
    if (!exporter) return null

    return <button type="button" onClick={() => void exporter.export()}>Export</button>
}

OrderExportBar renders nothing when no module above it registered OrderExporter.

Everything else matches useResolve: the same two modes with the same "nearest" default, the same snapshot of (resolver, token, mode), and the same refusal to read from a module that is not in a state to answer. A missing binding is tolerated; a dead module is not. See useResolve for those semantics in full.

Where to go next

Guides

Reference

MIT licensed.