Is it possible to remove the extra arrow function here?
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
Hi!
The tutorial has an example like this:
class Square extends React.Component {
render() {
return (
<button
className="square"
onClick={() => this.props.onClick()}
>
{this.props.value}
</button>
);
}
}
This component used in the Board component like this:
renderSquare(i) {
return (
<Square
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
/>
);
}
We have closed the context in a function that we pass to onClick in the Board component.
But why don't we just pass this.props.onClick to onClick in the Square component? Why are we using closures here if we already have the right context inside?
I wrote like this: (link to codepen)
class Square extends React.Component {
render() {
return (
<button
className="square"
onClick={this.props.onClick}
>
{this.props.value}
</button>
);
}
}
This works successfully
It is clear that the variant from the tutorial will also work. But further it says:
When we modified the Square to be a function component, we also changed onClick={() => this.props.onClick()} to a shorter onClick={props.onClick} (note the lack of parentheses on both sides).
So my variation only works in a functional component? But above it also worked in class components. I think this might confuse some people.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先閱讀關於提升 state 和函式元件的教學連結章節,接著比較兩個 onClick 範例及其附帶的說明。檢查類別元件範例是否需要釐清傳遞 callback 與呼叫 callback 之間的差異。當教學清楚說明兩種形式為何都能運作,以及較短的形式適用於何時,即視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100