@soujvnunes/react
v0.5.1
Published
React utilities: hooked-context factory, error boundary, motion helpers (subpath exports, optional peers).
Maintainers
Readme
@soujvnunes/react
React utilities as subpath exports with optional peers, so you import a subpath and install only what it needs. react is an optional peer; motion (for ./motion) is optional too.
./createHookedContext: context + guarded hook factory
createHookedContext<State>(name) returns { Context, useHook }: render <X.Context value={...}> (React 19 context-as-provider) and read with X.useHook(), which throws if used with no provider above. A private symbol sentinel means a state that legitimately is null never trips the "missing provider" error.
pnpm add @soujvnunes/react react'use client'
import { createHookedContext } from '@soujvnunes/react/createHookedContext'
export const Ring = createHookedContext<RingState>('Ring')
// render <Ring.Context value={ring}>, then read const ring = Ring.useHook()./ErrorBoundary: class error boundary with a Fallback prop
Catches render errors and renders the Fallback you pass (receiving { error, reset }); optional onError for logging. error carries Next's optional digest. A thrown value that is not an Error reaches both as an Error whose cause is the original, and the navigation errors from Next's redirect() and notFound() family are rethrown to Next's own boundary.
import { ErrorBoundary, type ErrorBoundaryFallbackProps } from '@soujvnunes/react/ErrorBoundary'
const Fallback = ({ error, reset }: ErrorBoundaryFallbackProps) => (
<button onClick={reset}>{error.message}</button>
)
// <ErrorBoundary Fallback={Fallback}>{children}</ErrorBoundary>./motion: motion helpers
isEmptyAnimatePresence(node) is true when a node is an <AnimatePresence> whose children are all falsy. It lets a layout drop wrapper markup around an empty presence.
pnpm add @soujvnunes/react react motionimport { isEmptyAnimatePresence } from '@soujvnunes/react/motion'
const visible = children.filter((child) => !isEmptyAnimatePresence(child))