reactjs / reactjs/react.dev

Render Props and PureComponent docs question/improvement opportunity

Offen
#825 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

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

https://github.com/reactjs/reactjs.org/blob/b4b68460e2377837df6e1826e77af173dc205f4f/content/docs/render-props.md

In the above link, the last section ("Caveats") talks about how one can avoid killing PureComponents benefits by pulling the render prop function out into an instance method. I understand why this is the case; the 'anonymous' function 'pointer' would change every render, so it isn't seen as something that didn't change, and the PureComponent would re-render all the time.

However, it says you can't do that with a PureComponent if the instance method / render prop needs to reference the component's state/props:

In cases where you cannot define the prop statically (e.g. because you need to close over the component's props and/or state) <Mouse> should extend React.Component instead.

I don't really understand why that is a limitation, so I'm proposing this as both a question and a suggestion for an update to the docs. I've never used or really looked into PureComponent, but from a look at the docs for them it seems like they re-render if a shallow comparison of it's props/state shows a difference. If that's the case, why wouldn't an instance function on Mouse that used it's own props/state work with PureComponent? The Mouse would re-render if one changed.

(Is it because the Cat - the child component of the render prop Mouse - wouldn't re-render, even though it's parent would?)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lies content/docs/render-props.md, insbesondere den abschließenden Abschnitt „Caveats“ und seine Hinweise zu PureComponent. Kläre, warum eine Instanzmethode als render prop, die props oder state verwendet, als inkompatibel mit PureComponent beschrieben wird, einschließlich der Frage, ob das Rendering-Verhalten des Kindelements eine Rolle spielt. Als erledigt gilt die Aufgabe, wenn die Dokumentation diese Frage direkt beantwortet und die relevante Einschränkung erklärt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.