Better implementation of render props
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
This page https://reactjs.org/docs/render-props.html introduces the "render props" pattern with a class Mouse. Its implementation is
render() {
return (
<div>
<h1>Move the mouse around! 1</h1>
<Mouse render={mouse => <Cat mouse={mouse} />} />
</div>
);
}
And its usage is
<Mouse render={mouse => <Cat mouse={mouse} />} />
I find that if we implement it as
render() {
const { render: Render } = this.props;
return (
<div
style={{ height: 100, border: "1px solid" }}
onMouseMove={this.handleMouseMove}
>
<Render mouse={this.state} />
</div>
);
}
Then we can allow the following usage patterns:
<Mouse render={({ mouse }) => <Cat mouse={mouse} />} /> // the original Cat class, 1st pattern
<Mouse render={MediumCat} /> // MediumCat is a function component, 2nd pattern
<Mouse render={BigCat} /> // BigCat is a class component, 3rd pattern
With this implementation, we have a pattern (the first one) which is almost the same as the original render props. Also it allows another two usage patterns (actually the 1st and 2nd patterns are the same).
I have created a codesandbox demo. In the demo, MouseTracker1 is the same as documented while MouseTracker2 is different in implementation of Mouse as I suggested.
I wonder if this is a better implementation of "render props". Any thought?
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 dalla pagina render-props collegata nell’issue e dalla demo CodeSandbox associata. Confronta l’implementazione documentata di Mouse con la variante proposta; il lavoro è concluso quando un maintainer ha deciso se la documentazione debba cambiare e quando è stato concordato l’ambito di qualsiasi aggiornamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 20/100