React useValue concept
by Carson Evans
React
function useValue(initialValue) {
const [value, setValue] = React.useState(initialValue);
return {
get value() {
return value;
},
set value(newValue) {
setValue(newValue);
}
};
}
function Counter({initialCount}) {
const count = useValue(initialCount);
return (
<React.Fragment>
<button onClick={() => count.value--}>-1</button>
{count.value}
<button onClick={() => count.value++}>+1</button>
</React.Fragment>
);
}
const root = ReactDOM.createRoot(document.body);
root.render(<Counter initialCount={5} />);