|
1 | | -import { useCallback, useMemo, useRef, useState } from "react"; |
| 1 | +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
2 | 2 | import { nearest } from "@react-md/utils"; |
3 | 3 |
|
4 | 4 | import { |
@@ -197,23 +197,25 @@ export function useRangeSlider( |
197 | 197 | }, [onChange, value]); |
198 | 198 |
|
199 | 199 | const prev = useRef({ min, max, step }); |
200 | | - if ( |
201 | | - prev.current.min !== min || |
202 | | - prev.current.max !== max || |
203 | | - prev.current.step !== step |
204 | | - ) { |
205 | | - // ensure that if the `min`, `max`, or `step` value changes that the value |
206 | | - // is updated as well. Without this, there will be a runtime error if the |
207 | | - // value is not within the new range. |
208 | | - prev.current = { min, max, step }; |
209 | | - const steps = getSteps(min, max, step); |
210 | | - const nextValue: RangeSliderValue = [ |
211 | | - nearest(value[0], min, max, steps), |
212 | | - nearest(value[1], min, max, steps), |
213 | | - ]; |
214 | | - currentValue.current = nextValue; |
215 | | - setValue(nextValue); |
216 | | - } |
| 200 | + useEffect(() => { |
| 201 | + if ( |
| 202 | + prev.current.min !== min || |
| 203 | + prev.current.max !== max || |
| 204 | + prev.current.step !== step |
| 205 | + ) { |
| 206 | + // ensure that if the `min`, `max`, or `step` value changes that the value |
| 207 | + // is updated as well. Without this, there will be a runtime error if the |
| 208 | + // value is not within the new range. |
| 209 | + prev.current = { min, max, step }; |
| 210 | + const steps = getSteps(min, max, step); |
| 211 | + const nextValue: RangeSliderValue = [ |
| 212 | + nearest(value[0], min, max, steps), |
| 213 | + nearest(value[1], min, max, steps), |
| 214 | + ]; |
| 215 | + currentValue.current = nextValue; |
| 216 | + setValue(nextValue); |
| 217 | + } |
| 218 | + }, [min, max, step, value]); |
217 | 219 |
|
218 | 220 | if (updateOn === "change" && currentValue.current !== value) { |
219 | 221 | currentValue.current = value; |
|
0 commit comments