reactjs / reactjs/react.dev

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

オープン
#5,832 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

「controlling a non react widget」のドキュメント例から始め、ここで提起されているケースに照らして、その useEffect/useRef のガイダンスを確認します。例の対象範囲、複数の refs、cleanup の動作を明示できるかどうかを判断します。完了の条件は、そのページでサポートされるパターンを明確にドキュメント化するか、より限定された対象範囲を記録することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
documentation
issue の種類
ドキュメント
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。