I apologize if this has been asked before, but I haven't found anything to help me quite yet, and I am new to React!
I want a value to increase every n number of seconds a user holds down a button. Right now it is just increasing linearly ever 35 milliseconds!
So far I have something like this:
function Increaser() {
const [value, setValue] = useState(0);
const changeTimer: React.MutableRefObject<any> = useRef(null);
function timeoutClearUp() {
clearInterval(changeTimer.current);
}
function increment() {
changeTimer.current = setInterval(() => setValue(prev => prev + 1), 35);
};
function onChange(e: any) {
setValue(parseInt(e.target.value));
}
return (
<div>
<button onMouseUp={timeoutClearUp} onMouseDown={increment}>INCREASE</button>
<input
type="number"
value={value}
onChange={(e) => {onChange(e)}}
/>
</div>
);
}
I have tried adding another ref but it didn't seem to work! What is a small thing I can add to this code to ensure that every second value is incremented by a larger and larger value (the longer a user holds the button down for).
Thanks so much!
- React: ^17.0.2
- Typecript: ^4.5.5
