繁体   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-2025 STACKOOM.COM