useRBox
useRBox
is a React hook that bridges the RBox
abstraction from F-Box with React’s ecosystem. It allows you to manage reactive state in React components with a declarative and functional programming approach.
The useRBox
hook integrates the reactivity of RBox
with React, enabling seamless state management for both local and global states. It leverages React’s useSyncExternalStore
for efficient updates and guarantees type safety with TypeScript.
Key Features
- Integration with
RBox
: Manage reactive state directly in React components. - Global and Local State: Supports both global shared state and component-specific state.
- Type Safety: Offers strong TypeScript support for predictable and error-free development.
Creating a useRBox
Hook
To use useRBox
, pass either an initial value, an RBox
instance, or a function that returns a value or RBox
.
Example
Supported Patterns
Local Reactive State
Use useRBox
to manage component-specific state.
Global Reactive State
Use useRBox
with a shared RBox
to manage state across multiple components.
API Methods
useRBox
useRBox<T>(source: T | RBox<T> | (() => T | RBox<T>), deps?: React.DependencyList): [T, RBox<T>]
Creates an RBox
and returns its value along with the RBox
instance.
Parameters
source
:- An initial value, an
RBox
instance, or a function returning a value orRBox
.
- An initial value, an
deps
:- A dependency array for memoization. The
RBox
is recomputed when dependencies change.
- A dependency array for memoization. The
Returns
value
:- The current value encapsulated in the
RBox
.
- The current value encapsulated in the
rbox
:- The
RBox
instance itself.
- The
Use Cases
Using RBox
for Local State
Manage local reactive state within a single component.
Using RBox
for Global State
Share state across multiple components using RBox
.
Access it in different components:
Why Use useRBox
?
The useRBox
hook provides a powerful and type-safe way to manage state in React applications. It combines the reactivity of RBox
with React’s component-based architecture, making it ideal for building scalable and maintainable applications.
Next Steps
Explore related topics to deepen your understanding:
- RBox API Reference: Learn about the
RBox
abstraction in F-Box. - useBox Reference: Understand how to work with static values in React using
useBox
. - useRBox Guide: Dive into advanced usage patterns for
useRBox
.