useBox
useBox
is a React hook that bridges the Box
abstraction from F-Box with React’s ecosystem. It allows you to work with encapsulated values in a functional and React-friendly way.
The useBox
hook simplifies managing static or derived values in React components. By integrating the Box
abstraction, it ensures immutability and composability while maintaining React’s declarative nature.
Key Features
- Integration with
Box
: Access and transformBox
values directly in React components. - Memoization: Avoid unnecessary computations with dependency-based reactivity.
- Type Safety: Enjoy strong TypeScript support for predictable and error-free usage.
Creating a useBox
Hook
To use useBox
, pass either a static value, a Box
instance, or a function that returns a value or Box
.
Example
Supported Patterns
Static Values
You can pass a static value to initialize the Box
.
Derived Values
Use a function to derive the Box
value dynamically.
Using an Existing Box
If you already have a Box
, pass it directly to useBox
.
API Methods
useBox
useBox<T>(source: T | Box<T> | (() => T | Box<T>), deps?: React.DependencyList): [T, Box<T>]
Creates a Box
and returns its value along with the Box
instance.
Parameters
source
:- A static value, a
Box
instance, or a function returning a value orBox
.
- A static value, a
deps
:- A dependency array for memoization. The
Box
is recomputed when dependencies change.
- A dependency array for memoization. The
Returns
value
:- The current value encapsulated in the
Box
.
- The current value encapsulated in the
box
:- The
Box
instance itself.
- The
Use Cases
Using Box
for Static Configurations
useBox
is ideal for managing global or static configurations that do not require frequent updates.
Deriving Component-Specific Values
Customize a global Box
for component-specific behavior.
Why Use useBox
?
The useBox
hook is a lightweight way to integrate functional programming with React. It allows you to encapsulate, transform, and reuse values across components while adhering to React’s declarative principles.
Next Steps
Explore related topics to deepen your understanding:
- Box API Reference: Learn about the
Box
abstraction in F-Box. - useRBox Reference: Manage reactive state efficiently with
useRBox
. - useBox Guide: Dive into advanced usage patterns for
useBox
.