[Suggestion]: Wrong example in fetching data section on You Might not need a Effect Page
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
Summary
In the Fetching Data Section of You might not need an effect is solving for different scenario with different problem altogether which will a create a confusion whether it requires a effect or not
Page
https://react.dev/learn/you-might-not-need-an-effect#fetching-data
Details
The Fetching Data Section of You might not need an effect is solving a different scenario with a different problem altogether.
In this code block:
function SearchResults({ query }) {
const [results, setResults] = useState([]);
const [page, setPage] = useState(1);
useEffect(() => {
// 🔴 Avoid: Fetching without cleanup logic
fetchResults(query, page).then(json => {
setResults(json);
});
}, [query, page]);
function handleNextPageClick() {
setPage(page + 1);
}
// ...
}
The documentation mentions that:
This might seem like a contradiction with the earlier examples where you needed to put the logic into the event handlers! However, consider that it's not the typing event that's the main reason to fetch. Search inputs are often prepopulated from the URL, and the user might navigate Back and Forward without touching the input.
It doesn't matter where page and query come from. While this component is visible, you want to keep results synchronized with data from the network for the current page and query. This is why it's an Effect.
This encourages people to add page as a dependency in the useEffect and also categorize the page change event as an effect altogether. But page changes are user events (wrt to the context like the results are shown in a table), and the documentation should encourage people to fetch data inside the change handler rather than using an effect (similar to how we encourage handling POST request on a form).
Regarding the URL scenario: let's say the user goes to page 3, navigates to a different page, and comes back again. The URL page param should be read inside the effect callback like this:
function SearchResults({ query }) {
const [results, setResults] = useState([]);
const [page, setPage] = useState(new URLSearchParams(window.location.search).get("page"));
useEffect(() => {
const urlSearchParams = new URLSearchParams(window.location.search);
const currentPage = urlSearchParams.get("page");
fetchResults(query, currentPage).then(json => {
setResults(json);
});
}, [query]);
function handleNextPageClick() {
setPage(page + 1);
fetchResults(query, page + 1).then(json => {
setResults(json);
});
}
// ...
}
This way, page can be removed as a dependency altogether, and the effect only runs whenever the query changes while always getting the latest page param value. When the page is changed by the user, the fetch should be invoked inside handleNextPageClick since page change is an event.
The earlier example will result in more confusion, and people will add things that should be inside the event handler into the effect with dependencies, making Event handlers sharing logic obsolete too. This also make the people aware on avoiding the "setState triggers effect" pattern for user events and follows the same principle on removing effects and using events properly as the article intended.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 You Might Not Need an Effect 頁面的 Fetching Data 區段開始,將其中的範例與周圍關於事件處理常式和共用邏輯的指引進行比較。檢視提議的 URL 和分頁情境,然後僅在說明始終明確區分使用者事件與同步時更新文件;完成標準是範例和周圍文字不再提供互相衝突的建議。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100