Better implementation of render props
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 11.8k
- Forks
- 7.9k
- Ø Merge
- 1 T. 11 Std.
- Gemergte PRs (30 T.)
- 11
Beschreibung
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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der im Issue verlinkten render-props-Seite und der dazugehörigen CodeSandbox-Demo. Vergleiche die dokumentierte Mouse-Implementierung mit der vorgeschlagenen Variante; abgeschlossen ist die Aufgabe, wenn ein Maintainer entschieden hat, ob sich die Dokumentation ändern sollte, und der Umfang eines etwaigen Updates abgestimmt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 20/100