Skip to content

Commit 8bb235b

Browse files
committed
[react] Add support for legacy context in class components in JSXElementConstructor
1 parent b031043 commit 8bb235b

6 files changed

Lines changed: 56 additions & 2 deletions

File tree

types/react/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ declare namespace React {
8383
*/
8484
deprecatedLegacyContext?: any,
8585
) => ReactNode)
86-
| (new (props: P) => Component<any, any>);
86+
| (new (
87+
props: P,
88+
/**
89+
* @deprecated https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods
90+
*/
91+
deprecatedLegacyContext?: any,
92+
) => Component<any, any>);
8793

8894
interface RefObject<T> {
8995
readonly current: T | null;

types/react/test/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,11 @@ const propsWithoutRef: React.PropsWithoutRef<UnionProps> = {
810810
return null;
811811
};
812812
Wrapper = (props, legacyContext: { foo: number }) => null;
813+
Wrapper = class Exact extends React.Component<ExactProps> {
814+
constructor(props: ExactProps, legacyContext: { foo: number }) {
815+
super(props, legacyContext);
816+
}
817+
};
813818

814819
React.createElement(Wrapper, { value: 'A' });
815820
React.createElement(Wrapper, { value: 'B' });

types/react/test/tsx.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,22 @@ class LegacyContext extends React.Component {
357357
}
358358
}
359359

360+
class LegacyContextWithConstructor extends React.Component {
361+
static contextTypes = { foo: PropTypes.node.isRequired };
362+
363+
constructor(props: {}, context: {}) {
364+
super(props, context);
365+
}
366+
367+
render() {
368+
// $ExpectType unknown
369+
this.context;
370+
return (this.context as any).foo;
371+
}
372+
}
373+
374+
<LegacyContextWithConstructor />;
375+
360376
class LegacyContextAnnotated extends React.Component {
361377
static contextTypes = { foo: PropTypes.node.isRequired };
362378
context: { foo: React.ReactNode } = { foo: {} as React.ReactNode };

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ declare namespace React {
5151
*/
5252
deprecatedLegacyContext?: any,
5353
) => ReactElement<any, any> | null)
54-
| (new (props: P) => Component<any, any>);
54+
| (new (
55+
props: P,
56+
/**
57+
* @deprecated https://legacy.reactjs.org/docs/legacy-context.html#referencing-context-in-lifecycle-methods
58+
*/
59+
deprecatedLegacyContext?: any,
60+
) => Component<any, any>);
5561

5662
interface RefObject<T> {
5763
readonly current: T | null;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ const propsWithoutRef: React.PropsWithoutRef<UnionProps> = {
818818
return null;
819819
};
820820
Wrapper = (props, legacyContext: { foo: number }) => null;
821+
Wrapper = class Exact extends React.Component<ExactProps> {
822+
constructor(props: ExactProps, legacyContext: { foo: number }) {
823+
super(props, legacyContext);
824+
}
825+
};
821826

822827
React.createElement(Wrapper, { value: 'A' });
823828
React.createElement(Wrapper, { value: 'B' });

types/react/ts5.0/test/tsx.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,22 @@ class LegacyContext extends React.Component {
357357
}
358358
}
359359

360+
class LegacyContextWithConstructor extends React.Component {
361+
static contextTypes = { foo: PropTypes.node.isRequired };
362+
363+
constructor(props: {}, context: {}) {
364+
super(props, context);
365+
}
366+
367+
render() {
368+
// $ExpectType unknown
369+
this.context;
370+
return (this.context as any).foo;
371+
}
372+
}
373+
374+
<LegacyContextWithConstructor />;
375+
360376
class LegacyContextAnnotated extends React.Component {
361377
static contextTypes = { foo: PropTypes.node.isRequired };
362378
context: { foo: React.ReactNode } = { foo: {} as React.ReactNode };

0 commit comments

Comments
 (0)