[Suggestion]: Fix the `data.js` file part of the solution for Challenge 4 in "Choosing the State Structure" chapter
還沒有人認領這個 Issue。
評估
- 難度
- 1/5
- 預估耗時
- 1 小時以內
- 新手友好度
- 70/100
- Issue 類型
- 文件
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- javascript, react
研究方向
開啟 Choosing the State Structure 頁面中連結的 Challenge 4 解決方案,並檢查其中的 data.js 檔案。確認提供的解決方案未使用 isStarred,然後驗證移除未使用的屬性後,顯示的 challenge 和回顧內容仍保持一致。
由索引模型根據 Issue 內容生成。
描述
Summary
The data.js provided in the solution for "Challenge 4" has a property called isStarred which is unused in the provided solution for the challenge:
export const letters = [{
id: 0,
subject: 'Ready for adventure?',
- isStarred: true,
}, {
Thus my suggestion is to remove the isStarred property from data.js file in challenge 4
Page
https://react.dev/learn/choosing-the-state-structure#recap
Details
The presence of the unused isStarred property can potentially confuse a beginner following the documentation, because they might have gone through the section on Avoiding Redundant States. I mean to say that they might end up writing a solution which utilises the isStarred property like shown below, which also solves the challenge:
import { useState } from 'react';
+import { letters as initialLetters } from './data.js';
import Letter from './Letter.js';
export default function MailClient() {
+ const [letters, setLetters] = useState(initialLetters);
+ const selectedCount = letters.filter(({isStarred}) => isStarred).length;
function handleToggle(toggledId) {
+ setLetters(letters => letters.map(letter => {
+ if (letter.id === toggledId) {
+ return {
+ ...letter,
+ isStarred: !letter.isStarred
+ }
+ } else return letter;
+ }))
}
return (
<>
<h2>Inbox</h2>
<ul>
{letters.map(letter => (
<Letter
key={letter.id}
letter={letter}
+ isSelected={letter.isStarred}
onToggle={handleToggle}
/>
))}
<hr />
<p>
<b>
You selected {selectedCount} letters
</b>
</p>
</ul>
</>
);
}
Although the above solution works, it has the following cons:
Tight Coupling: OverloadingisStarredfor both "selected" and "starred" behaviours creates coupling between two potentially distinct concepts. If the app later needs to treat "starred" and "selected" as separate attributes, refactoring will be necessary.Side Effects: ModifyingisStarredmight have unintended consequences elsewhere in the app if other features or components depend on it strictly representing "starred" status.
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 16 小時 6 分鐘
- 30 天內合併 PR
- 7
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
reactjs/react.dev 的其他 Issue
-
type: documentation
難度 2/5 1-3 小時 新手友好度 72/100
-
難度 1/5 1 小時以內 新手友好度 82/100
-
bug: unconfirmed
難度 2/5 1-3 小時 新手友好度 74/100
-
type: typos
難度 1/5 1 小時以內 新手友好度 90/100
-
bug: unconfirmed
難度 2/5 1-3 小時 新手友好度 68/100
查看 reactjs/react.dev 的全部 Issue
相似的 Issue
-
bot:ai-assisted component:compact-js status:untriaged
難度 2/5 1-3 小時 新手友好度 84/100
midnightntwrk/midnight-sdk#403 ·
-
難度 1/5 1 小時以內 新手友好度 92/100
-
難度 1/5 1-3 小時 新手友好度 86/100
DavidAnson/markdownlint-cli2#940 ·
-
documentation
難度 2/5 1-3 小時 新手友好度 88/100
githubnext/gh-aw-workshop#3692 ·
-
agent/guide documentation hive/hosted-available-lke648397-260827-5n31
難度 2/5 1-3 小時 新手友好度 90/100