A `useState` hook that can accept class instance as state.
npm install use-cls-stateA useState hook that can accept class instance as state.
``bash`
npm install --save use-cls-state
useClsState is just like useState, with additional capability to accept class instance as state.
`tsx
import { useClsState } from "use-cls-state";
class State {
public constructor(public counter: number) {}
public increment() {
this.counter = this.counter + 1;
}
}
function Page() {
const [state, setState] = useClsState(new State(123));
const increment = () => {
// Update class instance will not trigger re-render
state.increment();
// Re-render is only triggered when calling state setter
setState(state);
};
return (
{state.counter}
Be aware when using class instance as state, updating properties on the class instance will NOT trigger re-render. Re-render is only triggered when calling state setter.
© Cyan Ho (pilagod), 2024-NOW.
Released under the MIT License.