`useSyncExternalStore` Usage example question
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
Question Link
https://react.dev/reference/react/useSyncExternalStore#subscribing-to-a-browser-api
Summary
Hello!
I have a question about usage of useSyncExternalStore example
in this example, you can check navigator.onLine status using useSyncExternalStore.
Yeah, example works well, but i have one more question.
you can also implement this feature using useEffect
https://codesandbox.io/s/magical-http-qrpjss?file=/src/App.js:24-627
import { useEffect, useState } from "react";
export default function App() {
const [isOnline, setIsOnline] = useState(navigator.onLine);
const handleOnline = () => setIsOnline(true);
const handleOffline = () => setIsOnline(false);
useEffect(() => {
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);
return () => {
window.removeEventListener("online", handleOnline);
window.removeEventListener("offline", handleOffline);
};
}, []);
return <h1>{isOnline ? "✅ Online" : "❌ Disconnected"}</h1>;
}
It also works.
if window event can fire some action, we can implement same feature using setState without useSyncExternalStore so i need more explanation that why we use useSyncExternalStore in this situation.
it`s my sheer of curiosity.
can you explain more reason or example about this case?
Thank you!!
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從連結的 React 文件中關於 useSyncExternalStore 的項目開始,尤其是「subscribing to a browser API」範例,並將其與 issue 中的 useEffect 實作進行比較。當文件清楚說明範例為何使用 useSyncExternalStore,以及替代方案何時適用時,即視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 30/100