reactjs / reactjs/react.dev

Use case question regarding "You Probably Don't Need Derived State" article

Ouverte
#2,603 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
JavaScript
Étoiles
11.8k
Forks
7.9k
Merge moyen
1 j 11 h
PR mergées (30 j)
11

Description

Hello,

I just finished reading: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html and at the bottom of the article it states:

It is also worth re-iterating that getDerivedStateFromProps (and derived state in general) is an advanced feature and should be used sparingly because of this complexity. If your use case falls outside of these patterns, please share it with us on GitHub or Twitter!

My question regards complex components like Datatables which take in many arguments and which allow developers to externally change things about the table. For example, I may setup a table like this:

var options = {
    sortOrder: {field: "name", direction: "desc"},
}

<Datatable options={options} data={data} />

Internally the table allows users to change the sortOrder by clicking column headings. It manages to do this because it has it's own internal state which it derived from the input props. But lets say I want to change the sortOrder outside of the table.

One way to do this would be to change the options object and have the table re-render, it would then derive the sortOrder value from the props if present and use this for it's internal state. For table properties that aren't on props, the table would keep using it's internal state.

My question is - is this a good way of doing this, or is there a better way where state isn't derived from props? Comonents like a Datatable are very large and many of the properties should be uncontrolled, but there are certain properties that developers will sometimes want to control. Is this a good use case for using derived state?

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par relire l’article lié « You Probably Don't Need Derived State » ainsi que l’exemple de datatable dans cette issue. Déterminez si le mélange décrit de sortOrder contrôlé de l’extérieur et géré en interne est couvert par l’article ; le travail sera considéré comme terminé lorsqu’une réponse documentée ou une modification de la documentation clairement délimitée sera disponible.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, react
Domaine
documentation
Type d'issue
Documentation
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
20/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.