亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

傳遞了一個函數作為道具,但它沒有在 React 的子組件中執行

傳遞了一個函數作為道具,但它沒有在 React 的子組件中執行

收到一只叮咚 2021-12-02 19:34:45
我正在使用語義反應 ui 來制作菜單。利用活動狀態功能。在這種情況下,當您訪問鏈接時,它會獲得焦點或下劃線。然而,在我的實現中,我將它作為道具傳遞給子組件。見下文...class DesktopContainer extends Component { state = {} handleItemClick(event) {  var { name } = event.target;  this.setState({   activeItem: name  }) } render() {  const { GenericHeadingComponent, children, getWidth, isLoggedIn, logOutUser } = this.props  const { fixed, activeItem } = this.state  return (   <Responsive getWidth={getWidth} minWidth={Responsive.onlyTablet.minWidth}>        <GenericIsUserLoggedInLink         isHomeButton={true}         key="1"         name='home'         active={activeItem === 'home'}         handleItemClick={this.handleItemClick} /* is this correct */         mobile={false}        />        <GenericIsUserLoggedInLink         isLoggedIn={isLoggedIn}         route="/profile"         anchorText="Profile"         key="2"         name='profile'         active={activeItem === 'profile'}         handleItemClick={this.handleItemClick} /* is this correct */         mobile={false}        />        <GenericIsUserLoggedInLink         isLoggedIn={isLoggedIn}         route="/dashboard"         anchorText="Dashboard"         key="3"         name='dashboard'         active={activeItem === 'dashboard'}         handleItemClick={this.handleItemClick}  /* is this correct */         mobile={false}        />        <Menu.Item position='right'>         <Button inverted={!fixed}>          <GenericIsUserLoggedInLink           route="/login"           isLoggedIn={isLoggedIn}           logOutUser={logOutUser}           key="4" />         </Button>         <Button inverted={!fixed} primary={fixed} style={{ marginLeft: '0.5em' }}>          <Link href="/register">           <a>Register</a>          </Link>         </Button>        </Menu.Item>       </Container>      </Menu>      <GenericHeadingComponent />     </Segment>    </Visibility>    {children}   </Responsive>  ) }}我應該將狀態移到 HOC 中嗎?我沒有收到錯誤,所以我很難過。
查看完整描述

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//

}


查看完整回答
反對 回復 2021-12-02
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

你可以嘗試在構造函數中綁定你的方法嗎


class DesktopContainer extends Component {

  constructor(props) {

    super(props);

    this.state = {};

    this.handleItemClick = this.handleItemClick.bind(this);

  }

  // remaining code

}


希望能幫助到你


查看完整回答
反對 回復 2021-12-02
  • 2 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號