Skip to content

Commit

Permalink
[flow] Kill React$Component
Browse files Browse the repository at this point in the history
Summary: Changelog: [errors] Referencing `React$Component` directly will now be an error.

Reviewed By: jbrown215

Differential Revision: D70015939

fbshipit-source-id: 7da53dde3e84b579e1cfaf600d9f8b4c27809de4
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Feb 25, 2025
1 parent db61e9c commit d105351
Show file tree
Hide file tree
Showing 41 changed files with 1,341 additions and 1,344 deletions.
155 changes: 74 additions & 81 deletions lib/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,85 +22,6 @@ declare type React$Node =
| React$Portal
| Iterable<?React$Node>;


/**
* Base class of ES6 React classes, modeled as a polymorphic class whose main
* type parameters are Props and State.
*/
declare class React$Component<Props, State = void> {
// fields

props: Props;
state: State;

// action methods

setState(
partialState: ?$ReadOnly<Partial<State>> | ((State, Props) => ?$ReadOnly<Partial<State>>),
callback?: () => mixed,
): void;

forceUpdate(callback?: () => void): void;

// lifecycle methods

constructor(props: Props, context?: any): void;
render(): React$Node;
componentWillMount(): mixed;
UNSAFE_componentWillMount(): mixed;
componentDidMount(): mixed;
componentWillReceiveProps(
nextProps: Props,
nextContext: any,
): mixed;
UNSAFE_componentWillReceiveProps(
nextProps: Props,
nextContext: any,
): mixed;
shouldComponentUpdate(
nextProps: Props,
nextState: State,
nextContext: any,
): boolean;
componentWillUpdate(
nextProps: Props,
nextState: State,
nextContext: any,
): mixed;
UNSAFE_componentWillUpdate(
nextProps: Props,
nextState: State,
nextContext: any,
): mixed;
componentDidUpdate(
prevProps: Props,
prevState: State,
prevContext: any,
): mixed;
componentWillUnmount(): mixed;
componentDidCatch(
error: Error,
info: { componentStack: string, ... }
): mixed;

// long tail of other stuff not modeled very well

refs: any;
context: any;
getChildContext(): any;
static displayName?: ?string;
static childContextTypes: empty;
static contextTypes: empty;
static propTypes: any;

// We don't add a type for `defaultProps` so that its type may be entirely
// inferred when we diff the type for `defaultProps` with `Props`. Otherwise
// the user would need to define a type (which would be redundant) to override
// the type we provide here in the base class.
//
// static defaultProps: $Shape<Props>;
}

declare type React$AbstractComponentStatics = {
displayName?: ?string,
// This is only on function components, but trying to access name when
Expand Down Expand Up @@ -327,7 +248,7 @@ declare module react {
*
* @see https://react.dev/reference/react/Component
*/
declare export const Component: typeof React$Component;
declare export const Component: typeof React.Component;

/**
* `PureComponent` is similar to `Component`, but it skip re-renders with same props.
Expand Down Expand Up @@ -711,7 +632,79 @@ declare module react {
*
* @see https://react.dev/reference/react/Component
*/
declare const Component: typeof React$Component;
declare class Component<Props, State = void> {
// fields

props: Props;
state: State;

// action methods

setState(
partialState: ?$ReadOnly<Partial<State>> | ((State, Props) => ?$ReadOnly<Partial<State>>),
callback?: () => mixed,
): void;

forceUpdate(callback?: () => void): void;

// lifecycle methods

constructor(props: Props, context?: any): void;
render(): React$Node;
componentWillMount(): mixed;
UNSAFE_componentWillMount(): mixed;
componentDidMount(): mixed;
componentWillReceiveProps(
nextProps: Props,
nextContext: any,
): mixed;
UNSAFE_componentWillReceiveProps(
nextProps: Props,
nextContext: any,
): mixed;
shouldComponentUpdate(
nextProps: Props,
nextState: State,
nextContext: any,
): boolean;
componentWillUpdate(
nextProps: Props,
nextState: State,
nextContext: any,
): mixed;
UNSAFE_componentWillUpdate(
nextProps: Props,
nextState: State,
nextContext: any,
): mixed;
componentDidUpdate(
prevProps: Props,
prevState: State,
prevContext: any,
): mixed;
componentWillUnmount(): mixed;
componentDidCatch(
error: Error,
info: { componentStack: string, ... }
): mixed;

// long tail of other stuff not modeled very well

refs: any;
context: any;
getChildContext(): any;
static displayName?: ?string;
static childContextTypes: empty;
static contextTypes: empty;
static propTypes: any;

// We don't add a type for `defaultProps` so that its type may be entirely
// inferred when we diff the type for `defaultProps` with `Props`. Otherwise
// the user would need to define a type (which would be redundant) to override
// the type we provide here in the base class.
//
// static defaultProps: $Shape<Props>;
}

/**
* `PureComponent` is similar to `Component`, but it skip re-renders with same props.
Expand Down
2 changes: 1 addition & 1 deletion src/typing/__tests__/type_hint_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ let eval_hint_tests =
"jsx_props_of_class_component"
>:: mk_eval_hint_test_with_type_setup
~expected:"{+bar: string, +foo: number}"
"class MyComponent extends React$Component<{bar: string, foo: number}> {}; MyComponent"
"import * as React from 'react'; class MyComponent extends React.Component<{bar: string, foo: number}> {}; MyComponent"
[Decomp_JsxProps];
"jsx_props_of_function_component"
>:: mk_eval_hint_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ Flags: --pretty
},
{"name":"Range","type":"typeof Range"},
{"name":"RangeError","type":"typeof RangeError"},
{"name":"React$Component","type":"typeof React$Component"},
{
"name":"ReadableByteStreamController",
"type":"typeof ReadableByteStreamController"
Expand Down
4 changes: 2 additions & 2 deletions tests/badly_positioned_react/badly_positioned_react.exp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ References:
test.js:21:29
21| const Foo1 = create(create1<{||}>(Foo));
^^^^ [2]
<BUILTINS>/react.js:341:38
341| declare export type ComponentType<-P> = React$ComponentType<P>;
<BUILTINS>/react.js:262:38
262| declare export type ComponentType<-P> = React$ComponentType<P>;
^ [3]


Expand Down
Loading

0 comments on commit d105351

Please sign in to comment.