React Native 中 跨頁面間通信解決方案之 react-native-event-bus


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 );
    }
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM