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

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

在反應中調用作為道具傳遞的方法

在反應中調用作為道具傳遞的方法

牛魔王的故事 2022-12-22 15:49:28
我的父元素中有方法,di 將其作為道具傳遞;像這樣:<NavBar retrieveList={this.retrieveList}/>在我的子組件中,我無法從另一個方法體調用此方法。handleCloseModal () {    window.$('#newHireModal').modal('hide');   /* this is working */    console.log(this.props.retrieveList);     /* it gives method body, just to be sure props is coming here */    this.props.retrieveList;   /*it gives "Expected an assignment or function call..." */    this.props.retrieveList();   /*it gives "this.props.retrieveList is not a function" */    return this.props.retrieveList;   /*it does nothing. no working, no error. */  }順便說一句,我有構造函數和綁定;constructor(props) {    super(props);    this.handleCloseModal = this.handleCloseModal.bind(this);retrieveList() {    StaffDataService.getAll()      .then(response => {        this.setState({          staffWithBasics: response.data,          filteredItems: response.data,        });      })      .catch(e => {        console.log(e);      });  }這段代碼有什么問題,我該如何運行這個父方法?
查看完整描述

1 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

發生這種情況是因為this接收委托的組件中的this與函數中使用的不同。向下傳遞時,您必須綁定它。

<NavBar retrieveList={this.retrieveList.bind(this)}/>

然后你可以在你的函數體中調用它,如下所示。

this.props.retrieveList();


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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