reactjs / reactjs/react.dev

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

オープン
#3,122 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

この issue の TypeScript によるラッパーコンポーネントの例から始め、関数コンポーネントと children に関する React ウェブサイトの既存のドキュメントを確認します。適切なドキュメントのエントリーポイントを特定し、関連するガイダンスが、型付け、ReactNode、props.children、およびその動作がドキュメント化されているかどうかという4つの質問に答えていることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
react, typescript
領域
documentation
issue の種類
ドキュメント
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。