Learn how to create and update reactive state with just one hook. No Redux, no Context, no complexity.
A signal is like a smart variable that automatically tells your UI when it changes. It's the simplest way to manage state in React!
๐ฏ Think of it like this: A regular variable is silent when it changes. A signal shouts "Hey, I changed!" and React automatically updates your UI.
Creates a standalone signal outside components
// Outside component
const count = createSignal(0);
// Use anywhere!
function Display() {
const value = useSignalValue(count);
return <div>{value}</div>;
}โ Use for global state shared across components
React hook - creates signal inside component
// Inside component
function Counter() {
const [count, setCount] = useSignal(0);
return (
<button onClick={() => setCount(count + 1)}>
{count}
</button>
);
}โ Use for local state (like useState but reactive!)
๐ก In this demo: We use useSignal(0) because the counter is local to this component. For global state (like shopping cart), use createSignal()!
Because they make state management ridiculously simple!
const [count, setCount] = useSignal(0); // โ โ โ // value updater starting value
<div>{count}</div> // Just use it like a regular variable!setCount(count + 1) // UI updates automatically! โจ
๐ That's it! You're now using reactive state! Try it below โฌ๏ธ
๐ Try the buttons below!
Imagine you need a shopping cart quantity that multiple components can see and update:
// cart.js export const cartCount = createSignal(0);
// Header.jsx
const count =
useSignalValue(cartCount);
return <Badge>{count}</Badge>;// ProductCard.jsx cartCount.set( cartCount.get() + 1 );
// All components using // cartCount update // automatically! ๐
import { useSignal } from 'signalforge/react';const [count, setCount] = useSignal(0); // Start at 0{count} // Display the value
setCount(count + 1) // Update itimport { useSignal } from 'signalforge/react';
function Counter() {
// Create a signal with initial value 0
const [count, setCount] = useSignal(0);
return (
<div>
<h1>Count: {count}</h1>
{/* Update the signal */}
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button onClick={() => setCount(0)}>
Reset
</button>
</div>
);
}
// That's it! Your component will automatically
// re-render when count changes. No useEffect,
// no useCallback, no useMemo needed!Now that you understand basic signals, try these demos: