// hooks/useForceUpdate.ts
import { useState, useCallback } from "react"
export default function useForceUpdate() {
const [, setState] = useState({})
return useCallback (() => setState({}), [])
}
// components/Counter.tsx
import useForceUUpdate from "../hooks/useForceUpdate";
const state = {
count: 0
}
function increase() {
state.count += 1;
}
export default function Counter() {
const forceUpdate = useForceUUpdate()
const handleClick = () => {
increase()
forceUpdate()
}
return (
<div>
<p>{state.count}</p>
<button type="button" onClick={handleClick}></button>
</div>
)
}