From b3b075efae243589e1b8f7cbc54a2ccd24fc5591 Mon Sep 17 00:00:00 2001 From: Josh Headapohl Date: Wed, 28 Feb 2018 15:34:21 -0500 Subject: [PATCH] @types/react: Update React.Component setState Allow setState to accept an updater which is null or is a function that may return null. In React 16, passing null to setState or returning null from the updater function does not cause an update. --- types/react/index.d.ts | 2 +- types/react/test/tsx.tsx | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 93b143b7bf..05398e49b4 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -284,7 +284,7 @@ declare namespace React { // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257 // Also, the ` | S` allows intellisense to not be dumbisense setState( - state: ((prevState: Readonly, props: P) => (Pick | S)) | (Pick | S), + state: ((prevState: Readonly, props: P) => (Pick | S | null)) | (Pick | S | null), callback?: () => void ): void; diff --git a/types/react/test/tsx.tsx b/types/react/test/tsx.tsx index bc48add9a4..4ad5a3e2c8 100644 --- a/types/react/test/tsx.tsx +++ b/types/react/test/tsx.tsx @@ -95,6 +95,7 @@ class SetStateTest extends React.Component<{}, { foo: boolean, bar: boolean }> { this.setState({ foo: true }); this.setState({ foo: true, bar: true }); this.setState({}); + this.setState(null); this.setState({ foo: true, foo2: true }); // $ExpectError this.setState(() => ({ foo: '' })); // $ExpectError this.setState(() => ({ foo: true })); @@ -103,6 +104,7 @@ class SetStateTest extends React.Component<{}, { foo: boolean, bar: boolean }> { this.setState(() => ({ foo: '', foo2: true })); // $ExpectError this.setState(() => ({ })); // ok! this.setState({ foo: true, bar: undefined}); // $ExpectError + this.setState(prevState => (prevState.bar ? { bar: false } : null)); } }