statable
Version:
A small and lightning fast state management library for React and vanilla JavaScript.
34 lines (28 loc) • 713 B
JavaScript
import { useState, useEffect } from "react";
function Subscribe(props) {
const [, setState] = useState({});
let multi = false;
if (Array.isArray(props.to)) multi = true;
function onChange() {
setState({
changed: true
});
}
useEffect(() => {
if (!multi) {
props.to.subscribe(onChange);
return;
}
props.to.forEach(to => to.subscribe(onChange));
return () => {
if (!multi) {
props.to.unsubscribe(onChange);
return;
}
props.to.forEach(to => to.unsubscribe(onChange));
};
}, []);
if (!multi) return props.children(props.to.state);
return props.children(...props.to.map(to => to.state));
}
export default Subscribe;