1.0.0 β’ Published 6 months ago
react-signal-hook v1.0.0
react-use-signal π
A performant React hook for managing state transitions and deferred updates with surgical control.
Signals are renowned for their efficiency in performance-critical applications, enabling precise, minimal re-renders by targeting only specific components. This level of optimization has traditionally been unattainable within React's existing architecture. However, today marks a turning pointβnow, using only built-in React hooks, you can achieve unprecedented render minimization, significantly enhancing application performance.
Table of Contents π
- Features
- Installation
- Usage
- Examples
- Comparison with useState
- API Reference
- Performance
- Contributing
- License
π Features
- Automatic transition management
- Configurable deferred value strategy
- Zero-dependency implementation
- TypeScript first-class support
- React 18+ optimized
- DevTools integration
π¦ Installation
npm install react-use-signal
# or
yarn add react-use-signal
# or
pnpm add react-use-signal
# or
bun add react-use-signal
π Usage
Basic Example
import useSignal from "react-use-signal";
function App() {
const [text, setText] = useSignal("");
return (
<div>
<input
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Type here..."
/>
<Results query={text} />
</div>
);
}
Advanced Configuration
const [value, setValue] = useSignal(initialValue, {
smoothState: true, // Disable deferred value
noTransition: true, // Disable transition wrapping
});
π Examples
Search Input with Debouncing
function SearchBox() {
const [query, setQuery] = useSignal("");
const results = useMemo(() => searchAPI(deferredQuery), [deferredQuery]);
return (
<div>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<SearchResults results={results} />
</div>
);
}
Animation Frame Control
function AnimatedComponent() {
const [position, setPosition] = useSignal(0);
useFrame(() => {
setPosition((p) => p + 1);
});
return <div style={{ transform: `translateX(${position}px)` }} />;
}
βοΈ Comparison with useState
Code Complexity
useState | useSignal | |
---|---|---|
Lines of Code | 8 | 1 |
Transition Handling | Manual | Automatic |
Deferred Values | Manual | Built-in |
Update Strategies | 1 | 4 |
Performance Characteristics
Metric | useState | useSignal |
---|---|---|
Render Cycles | Higher | Optimized |
Input Responsiveness | Good | Better |
Jank Potential | Medium | Low |
When to Choose
Use Case | useState | useSignal |
---|---|---|
Simple state | β | β οΈ Overkill |
Complex transitions | β οΈ Manual | β Ideal |
High-frequency updates | β οΈ Possible jank | β Smooth |
Search/Filter UIs | β οΈ Possible lag | β Recommended |
π API Reference
useSignal(initialState, options?)
type Options = {
/**
* Bypass deferred value for immediate updates
* @default false
*/
smoothState?: boolean;
/**
* Disable React transition wrapping
* @default false
*/
noTransition?: boolean;
};
Return Value
Same as useState.
type ReturnType<S> = [state: S, setState: Dispatch<SetStateAction<S>>];
Configuration Modes
Mode | smoothState | noTransition | Use Case |
---|---|---|---|
Default | false | false | Optimized updates with transitions |
Immediate Updates | true | true | Critical state that needs instant reflection |
Transition Only | true | false | Batched updates without deferring |
Deferred Only | false | true | Late value without transition API |
ποΈ Performance
Optimization Strategies
- Debounced Updates: Automatic transition wrapping prevents UI blocking
- Deferred Rendering: Non-urgent updates don't block main thread
- Memoization Ready: Stable references for dependency arrays
π License
MIT Β© RB
Looking for maintainers! Help us improve this project - reach out if interested in maintaining.
1.0.0
6 months ago