Skip to content

[React 19] Get context value in class component constructor #7832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Lieblein opened this issue May 30, 2025 · 2 comments
Open

[React 19] Get context value in class component constructor #7832

Lieblein opened this issue May 30, 2025 · 2 comments

Comments

@Lieblein
Copy link

I updated version of react from 18.2.0 to 19.1.0 (@types/react to 19.1.6, @types/react-dom to 19.1.5).

After the update I received several components with such errors:

Argument of type 'typeof SplitCostDialog' is not assignable to parameter of type 'ComponentType<never>'.
  Type 'typeof SplitCostDialog' is not assignable to type 'ComponentClass<never, any>'.
    Target signature provides too few arguments. Expected 2 or more, but got 1.

I found the reason. I use context in class constructor.

export class SplitCostDialog extends React.PureComponent<ISplitCostDialogProps, IState> {
    static contextType = ResourcePlanContext;
    declare context: IResourcePlanContext;

    constructor(props: ISplitCostDialogProps, context: IResourcePlanContext) {
        super(props);

        const { scope, position } = context.modals.splitPositionCost;
        this.state = { form: new SplitCostForm(scope, position) };
    }

    ...

}

How can I use context inside a constructor after updating to version 19?
I didn't find the answer in the guide.

@rickhanlonii
Copy link
Member

Not sure how to type it correctly, but this code will work when you run it (though I think you should pass context to super too with super(props, context). @eps1lon do you know how to fix this type error?

@eps1lon
Copy link
Member

eps1lon commented Jun 3, 2025

The second argument got lumped in with legacy context way back when

We also need to update docs because it won't work if you don't call super(props, context)

Technically we should require the two-parameter overload if you specify contextType. But that may be hard to ship without breaking existing code.

I'll restore the old behavior in types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants