-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix React Strictmode for sliders #4012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
9ec8012
a61033a
a4e9e91
d56524c
9a345bd
37b8c69
1c9986e
502293b
53e3667
7f3fbfe
823b5e6
a3cdaa2
e98cffc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,9 @@ export function useSliderThumb( | |
}, [isFocused, focusInput]); | ||
|
||
const stateRef = useRef<SliderState>(null); | ||
stateRef.current = state; | ||
useEffect(() => { | ||
stateRef.current = state; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can get rid of |
||
let reverseX = direction === 'rtl'; | ||
let currentPosition = useRef<number>(null); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import {clamp, snapValueToStep} from '@react-aria/utils'; | |
import {Orientation} from '@react-types/shared'; | ||
import {SliderProps} from '@react-types/slider'; | ||
import {useControlledState} from '@react-stately/utils'; | ||
import {useMemo, useRef, useState} from 'react'; | ||
import {useEffect, useMemo, useRef, useState} from 'react'; | ||
|
||
export interface SliderState { | ||
/** | ||
|
@@ -192,9 +192,11 @@ export function useSliderState<T extends number | number[]>(props: SliderStateOp | |
const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined); | ||
|
||
const valuesRef = useRef<number[]>(null); | ||
valuesRef.current = values; | ||
const isDraggingsRef = useRef<boolean[]>(null); | ||
isDraggingsRef.current = isDraggings; | ||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't change this to a layouteffect since it's in stately and we can't use our SSR safe version there since it's in aria's utils There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It turned out that we don't even need to assign these refs during render. We only need them so that we can fire |
||
valuesRef.current = values; | ||
isDraggingsRef.current = isDraggings; | ||
}); | ||
|
||
function getValuePercent(value: number) { | ||
return (value - minValue) / (maxValue - minValue); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,46 +10,49 @@ | |
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {useCallback, useRef, useState} from 'react'; | ||
import {useCallback, useEffect, useRef, useState} from 'react'; | ||
|
||
export function useControlledState<T>( | ||
value: T, | ||
defaultValue: T, | ||
onChange: (value: T, ...args: any[]) => void | ||
): [T, (value: T, ...args: any[]) => void] { | ||
let [stateValue, setStateValue] = useState(value || defaultValue); | ||
let ref = useRef(value !== undefined); | ||
let wasControlled = ref.current; | ||
let isControlled = value !== undefined; | ||
// Internal state reference for useCallback | ||
let stateRef = useRef(stateValue); | ||
if (wasControlled !== isControlled) { | ||
console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`); | ||
} | ||
|
||
ref.current = isControlled; | ||
// detect if a component is switching from controlled to uncontrolled or vice versa, warn if so | ||
let isControlledRef = useRef(isControlled); | ||
useEffect(() => { | ||
let wasControlled = isControlledRef.current; | ||
if (isControlled !== wasControlled) { | ||
console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`); | ||
} | ||
isControlledRef.current = isControlled; | ||
}, [value, isControlled]); | ||
|
||
let setValue = useCallback((value, ...args) => { | ||
let onChangeCaller = (value, ...onChangeArgs) => { | ||
let setValue = useCallback((newValue, ...args) => { | ||
let onChangeCaller = (updateValue, ...onChangeArgs) => { | ||
if (onChange) { | ||
if (!Object.is(stateRef.current, value)) { | ||
onChange(value, ...onChangeArgs); | ||
if (!Object.is(stateRef.current, updateValue)) { | ||
onChange(updateValue, ...onChangeArgs); | ||
} | ||
} | ||
if (!isControlled) { | ||
stateRef.current = value; | ||
stateRef.current = updateValue; | ||
} | ||
}; | ||
|
||
if (typeof value === 'function') { | ||
if (typeof newValue === 'function') { | ||
console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320'); | ||
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates | ||
// when someone using useControlledState calls setControlledState(myFunc) | ||
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc | ||
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning | ||
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same | ||
let updateFunction = (oldValue, ...functionArgs) => { | ||
let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs); | ||
let interceptedValue = newValue(isControlled ? stateRef.current : oldValue, ...functionArgs); | ||
onChangeCaller(interceptedValue, ...args); | ||
if (!isControlled) { | ||
return interceptedValue; | ||
|
@@ -59,16 +62,19 @@ export function useControlledState<T>( | |
setStateValue(updateFunction); | ||
} else { | ||
if (!isControlled) { | ||
setStateValue(value); | ||
setStateValue(newValue); | ||
} | ||
onChangeCaller(value, ...args); | ||
onChangeCaller(newValue, ...args); | ||
} | ||
}, [isControlled, onChange]); | ||
|
||
// If a controlled component's value prop changes, we need to update stateRef | ||
if (isControlled) { | ||
stateRef.current = value; | ||
} else { | ||
useEffect(() => { | ||
// If a controlled component's value prop changes, we need to update stateRef | ||
if (isControlled) { | ||
stateRef.current = value; | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think this is too late. Imagine we have a controlled component. We fire onChange, this triggers a re-render of the parent component. Then another onChange fires before the useEffect is called, but we would be comparing against the previous value in For example, say you have two components: a parent and a child. The parent component has a Honestly, I'm not really sure how to solve this. For React 18, we might be able to use useInsertionEffect which runs before layout effects. But you'd still have the same problem if any other component used useInsertionEffect (rare), and for older Reacts. Updating during render is actually the most close to correct. It's only incorrect when a render gets aborted (e.g. during suspense). For strict mode, the value coming from props should be the same between the two renders so it shouldn't matter. Did you see test failures due to this, or was this just following the linter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Went back and checked the tests passing before and after. It's the same. So changing useControlledState was just following the linter. |
||
if (!isControlled) { | ||
value = stateValue; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.