Add/improve documentation for typing function components with access to children
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l'esempio di componente wrapper in TypeScript presente in questa issue e consulta la documentazione esistente del sito web di React sui componenti funzione e children. Individua il punto di ingresso appropriato nella documentazione, quindi assicurati che le indicazioni pertinenti rispondano alle quattro domande relative al tipaggio, a ReactNode, a props.children e al fatto che il comportamento sia documentato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- react, typescript
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 35/100