再不出來更新一下自己都感覺不到自己還存在了,這個監聽最常用的地方莫過於單選和全選了,,當然遠不止這個了,大家可以自己去多嘗試幾波,舉個栗子
A組件全選所在
//全選 choose(bool){ //選中 if(bool){ DeviceEventEmitter.emit('allChoose’); }else{ //全不選 DeviceEventEmitter.emit('allNoChoose’); }
B組件單選所在地
componentDidMount(){ //監第二個參數是函數, this.subscription =DeviceEventEmitter.addListener('allChoose’,this.chooseSomething.bind(this)); }; chooseSomething(){ //這里就可以做一些事情啦 } //不用了記得移除 componentWillUnmount(){ this.subscription.remove(); };
就是這么粗暴,不用傳來傳去,當然頭部還是要引入這個文件的 import {DeviceEventEmitter} from 'react-native';