Add/improve documentation for typing function components with access to children
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>
);
}
This seems to work, but I have a couple of questions:
- Is this the best practice to model wrapper components with children?
- Is
ReactNodethe correct, most general type forchildren? - How does React automagically know that the children are assigned to
props.children? Is this hardwired to the namechildrenor is there anything I should be aware of? - 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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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