Add `startTransition` as a known stable method (#19720)

The `startTransition` method returned from `useTransition` is a stable
method, like `dispatch` or `setState`. You should not have to specify
it as a hook dependency.
This commit is contained in:
Andrew Clark 2020-08-28 18:44:39 -05:00 committed by GitHub
parent 4f5fb56100
commit a8500be893
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -603,6 +603,10 @@ const tests = {
const [state4, dispatch2] = React.useReducer();
const [state5, maybeSetState] = useFunnyState();
const [state6, maybeDispatch] = useFunnyReducer();
const [startTransition1] = useTransition();
const [startTransition2, isPending2] = useTransition();
const [startTransition3] = React.useTransition();
const [startTransition4, isPending4] = React.useTransition();
const mySetState = useCallback(() => {}, []);
let myDispatch = useCallback(() => {}, []);
@ -616,6 +620,10 @@ const tests = {
setState2();
dispatch1();
dispatch2();
startTransition1();
startTransition2();
startTransition3();
startTransition4();
// Dynamic
console.log(state1);
@ -624,6 +632,8 @@ const tests = {
console.log(state4);
console.log(state5);
console.log(state6);
console.log(isPending2);
console.log(isPending4);
mySetState();
myDispatch();
@ -634,6 +644,7 @@ const tests = {
// Dynamic
state1, state2, state3, state4, state5, state6,
maybeRef1, maybeRef2,
isPending2, isPending4,
// Not sure; assume dynamic
mySetState, myDispatch,

View File

@ -400,6 +400,17 @@ export default {
return false;
}
}
} else if (name === 'useTransition') {
if (
id.type === 'ArrayPattern' &&
Array.isArray(resolved.identifiers)
) {
// Is first tuple value the same reference we're checking?
if (id.elements[0] === resolved.identifiers[0]) {
// Setter is stable.
return true;
}
}
}
// By default assume it's dynamic.
return false;