Lifecycle Hooks
Smithers provides vendored lifecycle hooks fromreact-use that are clearer and more robust than raw useEffect.
useMount
Runs a callback exactly once when the component mounts.Why Use useMount?
More robust thanuseEffect(() => fn(), []) because it:
- Clearly communicates mount-only intent
- Is easier to grep for mount behavior
- Uses an idempotency guard to avoid strict mode replay double-runs
API
useUnmount
Runs a callback when the component unmounts. More robust thanuseEffect cleanup because it always calls the latest version of the callback via ref.
Why Use useUnmount?
Avoids stale closure issues that plague normal cleanup functions:API
useMountedState
Returns a function that tells you if the component is currently mounted. Essential for avoiding “setState on unmounted component” warnings in async code.API
useEffectOnValueChange
Runs an effect when a value changes, with idempotency guarantees. UnlikeuseEffect with [value], this:
- Won’t run twice for the same value (handles React strict mode)
- Updates the “last seen” value synchronously before running the effect
- Runs on first mount when value first becomes available
API
When to Use Each Hook
| Hook | Use Case |
|---|---|
useMount | Code that runs once when component mounts |
useUnmount | Cleanup that needs latest props/state (avoids stale closures) |
useMountedState | Async operations that set state (prevents unmounted setState) |
useEffectOnValueChange | Re-run on dependency changes with idempotency |
Raw useEffect | Only when you need standard React effect behavior |
Pattern: Component Execution
Smithers components execute themselves viauseMount: