reactjs / reactjs/react.dev

Can "controlling a non react widget" be made clearer?

Ouverte
#5,832 1 commentaire 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

As a reader, I find the example very concise and basic. It is not clear what to do with the same pattern as soon as it grows out of scope:

  • an instance of a class such as MapWidget having more than one method
  • an instance of a class such as MapWidget subscribes to events

MapWidget has a constructor and setZoom, it may not be clear why the "zoom" state is of interest when constructing the widget.

A hypothetical example that would have more methods could make it more obvious that all those methods should be called in a single effect and that this effect should manage two refs - one for a dom element, and one for an instance of a class that depends on said dom element.

I'm not sure if the issue of documenting how to actually perform cleanup in a pattern such as this would invalidate this example though:

  const containerRef = useRef(null);
  const mapRef = useRef(null);
  
  // an effect that is just concerned about creating an instance of a class with a dom element dependency
  // cleanup(), destroy(), dispose() or noop() make sense to be called
  useEffect(() => {
    if (mapRef.current === null) {
      mapRef.current = new MapWidget(containerRef.current);
    } 
    return ()=>{
      if(mapRef.current) mapRef.current.dispose()
    }
  },[])
  
  // an effect that just works on a map ref, doesn't care about any dom elements and such
  useEffect(() => {
    const map = mapRef.current;
    if(!map) return
    map.setFoo(foo);
    map.setBar(bar);
    map.setBaz(baz);
    map.setQux(qux);
  }, [foo,bar,baz,qux]);

I myself find it hard to reconcile certain concepts that i've picked up over the years and bits and pieces of best practices i pick up, mostly on twitter i guess. One such is that useMount is not advised, and imagine that reasoning is why this example doesn't have the empty dep effect (but other sections do?).

I'm afraid to make any suggestions, but i think that an example running an animation with the canvas could be a valuable example:

  • a canvas 2d widget would require the ref from the <canvas/> element
  • a circle going up and down in a square area would require an raf loop - creating a need for something to be cleaned up.
  • a toggle could change the color of the circle
  • another toggle could change the shape of the circle to a star

The way effects would be grouped in such an example could give more data points what the best practices are with this pattern.

Probably out of scope - but it's also interesting to know what should be done in case the dom element ref is for whatever reason null if that is still possible with modern react.

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 l’exemple de documentation « controlling a non react widget » et examinez ses indications sur useEffect/useRef à la lumière des cas soulevés ici. Déterminez si la portée de l’exemple, l’utilisation de plusieurs refs et le comportement de cleanup peuvent être explicités ; le travail est terminé lorsque la page documente clairement le modèle pris en charge ou consigne une portée plus limitée.

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
25/100

Recevez les nouvelles issues par e-mail

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