const Dashboard: React.FC = () => {
const store = createStore(countReducer)
const divRef = useRef<HTMLDivElement>(null);
store.subscribe(() => {
const state = store.getState()
if (divRef.current)
divRef.current.innerText = `this is cool, another listener: ${state.count}`;
})
return (
...
<button onClick={() => store.dispatch({ type: ActionType.INCREMENT })}>add</button>
<button onClick={() => store.dispatch({ type: ActionType.DECREMENT })}>minus</button>
<div ref={divRef}>0</div>
...
)
}
Re-Render:
const count = useRef(0);
useEffect(() => {
count.current = count.current + 1;
});
return (
<>
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
<h1>Render Count: {count.current}</h1>
</>
);