reactjs / reactjs/react.dev

Add/improve documentation for typing function components with access to children

Đang mở
#3,122 0 bình luận 0 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ả

With the introduction of React Hooks, the documentation recommends using function components instead of class components in the future.

However, I wonder how to define and type the component's properties when I want to access children components. I have come up with this solution (I am using Typescript):

import * as React from "react";
import "./styles.css";

type MyWrapperCompProps = {
  color: string;
  children?: React.ReactNode;
};

function MyWrapperComp(props: MyWrapperCompProps) {
  return <div style={{ backgroundColor: props.color }}>{props.children}</div>;
}

export default function App() {
  return (
    <div className="App">
      <MyWrapperComp color="tan">
        <h1>Hello World!</h1>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Porro, fuga.
        </p>
      </MyWrapperComp>
    </div>
  );
}

Edit distracted-sunset-qgwpo

This seems to work, but I have a couple of questions:

  1. Is this the best practice to model wrapper components with children?
  2. Is ReactNode the correct, most general type for children?
  3. How does React automagically know that the children are assigned to props.children? Is this hardwired to the name children or is there anything I should be aware of?
  4. Is the above documented anywhere at all on the React website?

Thanks a lot in advance for your help.

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 ví dụ về component wrapper bằng TypeScript trong issue này và xem lại tài liệu hiện có trên trang web React về function component và children. Xác định điểm bắt đầu phù hợp trong tài liệu, sau đó đảm bảo rằng phần hướng dẫn liên quan trả lời bốn câu hỏi về việc định kiểu, ReactNode, props.children và liệu hành vi đó có được ghi lại trong tài liệu hay không.

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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
35/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.