<input
ref={left_slider}
onClick={() => set_neg_clicked(!neg_clicked)}
step={100}
type="range"
min={-1000}
max={neg_side_max}
value={neg_side_val}
onChange={(e) => set_neg_side_val(Math.min(900, e.target.value))}
className="slider left"
id="my_neg_range"
style={{
margin: 0,
width: `${neg_side_width}px`,
position: "absolute",
left: 0,
zIndex: 2,
}}
></input>
I need to know when the thumb is clicked. So how can the thumb be accessed in React?
It's an interesting question and the straight answer to that is it's not directly accessible. You have a few options though.
The limitation is when you click and not leave the mouse, that's not a complete click.
Here is my crazy idea as code,
import React, { useState, useRef } from 'react';
export default function App() {
const [value, setValue] = useState(1);
let clickRef = useRef(false);
return (
<input
type="range"
min="1"
max="100"
value={value}
onInput={e => {
setValue(+e.target.value);
clickRef.current = true;
}}
onClick={e => {
if (!clickRef.current) console.log('Clicked thump');
clickRef.current = false;
}}
/>
);
}
I used useRef to hold a value, not as an element ref. Changing the value of ref won't trigger a re-render.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With