Skip to content

Commit 70f4570

Browse files
authored
[react] Restore context parameter in legacy class Component lifecycles (DefinitelyTyped#72216)
1 parent e0e1168 commit 70f4570

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

types/react-addons-shallow-compare/react-addons-shallow-compare-tests.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import shallowCompare = require("react-addons-shallow-compare");
33

44
export class MyComponent extends React.Component {
5-
shouldComponentUpdate(nextProps: any, nextState: any): boolean {
5+
shouldComponentUpdate(nextProps: any, nextState: any, nextContext: any): boolean {
66
return shallowCompare(this, nextProps, nextState);
77
}
88

types/react-lifecycle-component/react-lifecycle-component-tests.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,27 @@ function mapDispatchToProps(dispatch: Dispatch): MapDispatchProps {
6464
componentDidMount() {
6565
dispatch({ type: "ComponentDidMount" });
6666
},
67-
componentWillUpdate(nextProps, nextState) {
67+
componentWillUpdate(nextProps, nextState, nextContext) {
6868
const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo;
69+
const hasNextContext: boolean = !!nextContext;
6970
dispatch({ type: "ComponentWillUpdate" });
7071
},
7172
componentDidUpdate(nextProps, nextState, nextContext) {
7273
const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo;
7374
const hasNextContext: boolean = !!nextContext;
7475
dispatch({ type: "ComponentDidUpdate" });
7576
},
76-
componentWillReceiveProps(nextProps) {
77+
componentWillReceiveProps(nextProps, nextContext) {
7778
const fooIsGreaterThanZero: boolean = nextProps.propsFoo > 0;
79+
const hasNextContext: boolean = !!nextContext;
7880
dispatch({ type: "ComponentWillReceiveProps" });
7981
},
8082
componentWillUnmount() {
8183
dispatch({ type: "ComponentWillUnmount" });
8284
},
83-
shouldComponentUpdate(nextProps, nextState) {
85+
shouldComponentUpdate(nextProps, nextState, nextContext) {
8486
const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo;
87+
const hasNextContext: boolean = !!nextContext;
8588
return !fooIsEqual;
8689
},
8790
};

types/react/index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ declare namespace React {
11771177
* If false is returned, {@link Component.render}, `componentWillUpdate`
11781178
* and `componentDidUpdate` will not be called.
11791179
*/
1180-
shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>): boolean;
1180+
shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean;
11811181
/**
11821182
* Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as
11831183
* cancelled network requests, or cleaning up any DOM elements created in `componentDidMount`.
@@ -1276,7 +1276,7 @@ declare namespace React {
12761276
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
12771277
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
12781278
*/
1279-
componentWillReceiveProps?(nextProps: Readonly<P>): void;
1279+
componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
12801280
/**
12811281
* Called when the component may be receiving new props.
12821282
* React may call this even if props have not changed, so be sure to compare new and existing
@@ -1294,7 +1294,7 @@ declare namespace React {
12941294
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
12951295
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
12961296
*/
1297-
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>): void;
1297+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
12981298
/**
12991299
* Called immediately before rendering when new props or state is received. Not called for the initial render.
13001300
*
@@ -1308,7 +1308,7 @@ declare namespace React {
13081308
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
13091309
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
13101310
*/
1311-
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>): void;
1311+
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
13121312
/**
13131313
* Called immediately before rendering when new props or state is received. Not called for the initial render.
13141314
*
@@ -1324,7 +1324,7 @@ declare namespace React {
13241324
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
13251325
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
13261326
*/
1327-
UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>): void;
1327+
UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
13281328
}
13291329

13301330
function createRef<T>(): RefObject<T | null>;

types/react/ts5.0/index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ declare namespace React {
11761176
* If false is returned, {@link Component.render}, `componentWillUpdate`
11771177
* and `componentDidUpdate` will not be called.
11781178
*/
1179-
shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>): boolean;
1179+
shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean;
11801180
/**
11811181
* Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as
11821182
* cancelled network requests, or cleaning up any DOM elements created in `componentDidMount`.
@@ -1275,7 +1275,7 @@ declare namespace React {
12751275
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
12761276
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
12771277
*/
1278-
componentWillReceiveProps?(nextProps: Readonly<P>): void;
1278+
componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
12791279
/**
12801280
* Called when the component may be receiving new props.
12811281
* React may call this even if props have not changed, so be sure to compare new and existing
@@ -1293,7 +1293,7 @@ declare namespace React {
12931293
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props}
12941294
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
12951295
*/
1296-
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>): void;
1296+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
12971297
/**
12981298
* Called immediately before rendering when new props or state is received. Not called for the initial render.
12991299
*
@@ -1307,7 +1307,7 @@ declare namespace React {
13071307
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
13081308
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
13091309
*/
1310-
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>): void;
1310+
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
13111311
/**
13121312
* Called immediately before rendering when new props or state is received. Not called for the initial render.
13131313
*
@@ -1323,7 +1323,7 @@ declare namespace React {
13231323
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#reading-dom-properties-before-an-update}
13241324
* @see {@link https://legacy.reactjs.org/blog/2018/03/27/update-on-async-rendering.html#gradual-migration-path}
13251325
*/
1326-
UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>): void;
1326+
UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
13271327
}
13281328

13291329
function createRef<T>(): RefObject<T | null>;

0 commit comments

Comments
 (0)