Child component
export const FlightRange = (props) => {
const [value, setValue] = useState(props.value);
return (
<>
<input
type='range'
min={1000}
max={50000}
step="500"
value={value}
onChange={(e) => {
setValue(e.target.value);
props.handleSliderChange(value);
}}
/>
<span>{value}</span>
</>
);
};
parent component
useEffect(() => {
const result = axios.get('http://localhost:8000/')
.then((res) => res.json())
.then((data) => {
const flightData = data.filter((value) => {
return (
valuesplit(' ')[1] < priceSlider
);
});
})
}, [priceSlider]);
return(
<Child value={priceSlider} handleSliderChange={(value)=> setPriceSlider(value)} />
)
useEffect does not get called when the slider is changed the first time. It gets called a second time with the stale (previous value) value. What am I missing?
in
onChange
you need to call like thissince value is not updated instantly when you call
setValue(e.target.value);
,value
will have previous value that you are passing inprops.handleSliderChang(value)
to know how
setState
works see this answer