首页 > 其他 > 详细

React Native 中 跨页面间通信解决方案之 react-native-event-bus

时间:2019-10-11 14:49:30      阅读:323      评论:0      收藏:0      [点我收藏+]

https://github.com/crazycodeboy/react-native-event-bus

用法: A页面和B页面中都有相同的列表,点击B页面中的收藏按钮,A页面会跟着更新

import EventBus from ‘react-native-event-bus‘;

 

constructor(props){
    super(props);
    const {tabLabel} = this.props;
    this.storeName = tabLabel;
    this.isFavoriteChanged = false;
  }

  componentDidMount(){
    this.loadData();
    EventBus.getInstance().addListener(EventTypes.favorite_changed_popular,this.favoriteChangeListener = ()=>{
      this.isFavoriteChanged = true;
    });
    EventBus.getInstance().addListener(EventTypes.bottom_tab_select,this.bottomTabSelectListener = (data)=>{
      if(data.to === 0 && this.isFavoriteChanged){
        this.loadData(null,true);
      }
    });
  }

  componentWillUnmount(){
    EventBus.getInstance().removeListener(this.favoriteChangeListener);
    EventBus.getInstance().removeListener(this.bottomTabSelectListener);
  }
onFavorite(item,isFavorite){
    
    FavoriteUtil.onFavorite(this.favoriteDao,item,isFavorite,this.props.flag);
    if(this.storeName === FLAG_STORAGE.flag_popular){
      EventBus.getInstance().fireEvent(EventTypes.favorite_changed_popular);
    }
    else{
      EventBus.getInstance().fireEvent(EventTypes.favorite_changed_trending );
    }
  }

 

React Native 中 跨页面间通信解决方案之 react-native-event-bus

原文:https://www.cnblogs.com/liuw-flexi/p/11653607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!