@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.
This commit is contained in:
Josh Headapohl
2018-02-28 19:39:53 -05:00
parent 4bab2f7783
commit b3b075efae
2 changed files with 3 additions and 1 deletions
+1 -1
View File
@@ -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<K extends keyof S>(
state: ((prevState: Readonly<S>, props: P) => (Pick<S, K> | S)) | (Pick<S, K> | S),
state: ((prevState: Readonly<S>, props: P) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null),
callback?: () => void
): void;
+2
View File
@@ -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));
}
}