subscribe
Method to manually subscribe to state changes with explicit unsubscribe control.
Description
The .subscribe()
method provides manual subscription management for React Signify state changes. It returns an unsubscribe function that allows you to control when to stop listening to changes.
Input/Output
Input | Type | Description |
---|---|---|
callback | (value: T) => void | Function to execute when state changes |
Output | Type | Description |
---|---|---|
Returns | { unsubscribe: () => void } | Object containing unsubscribe function |
Examples
tsx
import { signify } from "react-signify";
const count = signify(0);
function MyComponent() {
useEffect(() => {
const { unsubscribe } = count.subscribe((newCount) => {
console.log("Count is now:", newCount);
});
return unsubscribe; // Cleanup when component unmounts
}, []);
return <div>My Component</div>;
}