Skip to content

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

Open
@Lieblein

Description

@Lieblein

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions