2 回答

TA貢獻1804條經驗 獲得超3個贊
首先,您的 handleClick 在設置狀態時會由于this. 所以我建議你要么在構造函數中綁定它,要么把它改成這樣的箭頭函數 -
handleItemClick = (e) => {}
現在,我已經在幾個項目中使用了 Semantic-ui-react,并且點擊事件的運行方式有點不同。根據文檔 -
Called on click. When passed, the component will render as an `a`
tag by default instead of a `div`.
onClick(event: SyntheticEvent, data: object)
event
React's original SyntheticEvent.
data
All props.
所以改變你的 onClick 到onClick={handleItemClick}then
handleItemClick = (e, { name }) => this.setState({ activeItem: name })
我希望這可以幫助你。
更新:您的最新錯誤是因為<a>鏈接中的標簽。鏈接來自react-router就像一個標簽,我們知道
<a href="1">
<a href="2"></a>
</a>
是無效的 HTML。您可以參考此問題了解更多信息Link 不能作為鏈接的后代出現。
要解決您的錯誤,只需刪除標簽內的標簽即可。
更新: -
最新的錯誤是因為您在 handleItemClick 中傳遞名稱
if (isHomeButton) {
return <Link href="/"><Menu.Item name={name} active={activeItem === name} onClick={handleItemClick}></Menu.Item></Link> //change the onClick here//
}

TA貢獻1895條經驗 獲得超3個贊
你可以嘗試在構造函數中綁定你的方法嗎
class DesktopContainer extends Component {
constructor(props) {
super(props);
this.state = {};
this.handleItemClick = this.handleItemClick.bind(this);
}
// remaining code
}
希望能幫助到你
添加回答
舉報