簡體   English   中英

React-Native引用未定義

[英]React-Native Refs are Undefined

我目前正在開發應用程序,以下是與我的錯誤相關的基本代碼:

這是渲染函數:

render() {
 return (
  <ScrollableTabView>
    <NavigatorIOS
      translucent={false}
      initialRoute={{
        component: MenuList,
        title: '',
        passProps: { hideNavBar: this.hideNavBar,
                     showNavBar: this.showNavBar,
                     toggleFav: this.toggleFav,
                     getFavImg: this.getFavImg,
                    },
      }}
      navigationBarHidden={this.state.hideNavBar}
      style={{ flex: 1 }}
      barTintColor="#1f3157"
      titleTextColor="#f6f6f6"
      tabLabel="Menu"
    />
    <NavigatorIOS
      ref="nav"
      translucent={false}
      initialRoute={{
        component: FavoritesList,
        title: '',
        passProps: { hideNavBar: this.hideNavBar,
                     showNavBar: this.showNavBar,
                     favs: this.state.favs,
                   },
      }}
      navigationBarHidden={this.state.hideNavBar}
      style={{ flex: 1 }}
      barTintColor="#1f3157"
      titleTextColor="#f6f6f6"
      tabLabel="Favorites"
    />
    <SpecialsList tabLabel="Specials" />
  </ScrollableTabView>
);

因此,現在,當用戶在第一個NavigatorIOS中更新某些內容時,我正在嘗試進行第二個NavigatorIOS更新。 我目前擁有它,因此當第一個更新時,它會調用一個函數,然后依次調用一個函數來替換第二個:

toggleFav = ( item ) => {
 //if not a favorite / not in map
 if ( !this.state.favs[item.title] ){
  this.state.favs[item.title] = item;
  this.state.favs[item.title].faved = true;
 }
 else if ( this.state.favs[item.title].faved ){
  this.state.favs[item.title].faved = false;
 }
 else {
  this.state.favs[item.title].faved = true;
 }

 this.favChange();
}

favChange = () => {
 this.refs.nav.replace({
        component: FavoritesList,
        title: '',
        passProps: {
              hideNavBar: this.hideNavBar,
              showNavBar: this.showNavBar,
              favs: this.state.favs,
        },
      });
}

我得到現在的錯誤是this.refs.nav在未定義favChange功能。 我知道this一定是一個問題,但是我不知道如何解決它。

提前致謝!

可能是因為“此”綁定。 this.favChange = this.favChange.bind(this)是否可以嘗試在構造函數或初始化代碼中添加此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM