Can "controlling a non react widget" be made clearer?
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 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
MapWidgethaving more than one method - an instance of a class such as
MapWidgetsubscribes 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.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從「controlling a non react widget」文件範例開始,針對這裡提出的情況,檢視其中關於 useEffect/useRef 的指引。判斷是否能明確說明範例的範圍、多個 refs 以及 cleanup 行為;完成的標準是該頁面清楚記錄支援的模式,或記錄較窄的範圍。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 25/100