reactjs / reactjs/react.dev

[React 19] Get context value in class component constructor

Đang mở
#7,832 4 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
JavaScript
Star
11.8k
Fork
7.9k
Merge trung bình
1 ngày 11 giờ
Pull request đã merge (30 ngày)
11

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với hướng dẫn nâng cấp React 19 được liên kết trong issue và so sánh hướng dẫn này với ví dụ constructor của class component đã được báo cáo. Ghi lại cách xử lý việc truy cập context trong các constructor sau khi nâng cấp, bao gồm hướng dẫn migration liên quan, để hướng dẫn giải đáp rõ ràng trường hợp này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
react, typescript
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.