meinto / react-native-event-listeners Goto Github PK
View Code? Open in Web Editor NEWglobal event listener extension for react or react-native
License: MIT License
global event listener extension for react or react-native
License: MIT License
We are using this for token refresh. when token is refreshed an event is emitted. On IOS, we get a crash when phone is locked for a little while. Can you please advise why that is the case or is removing listeners really important?
declare module "react-native-event-listeners" {
class EventRegister {
static addEventListener(eventName: string, callBack: () => void): string | boolean
static removeEventListener(listenerRef: any): boolean
static removeAllListeners(): boolean
static emitEvent(eventName: string): void
static on(eventName: string, callBack: () => void): string | boolean
static rm(listenerRef: any): boolean
static rmAll(): boolean
static emit(eventName: string): void
}
export { EventRegister }
}
@meinto, thanks to bring it to us!
Closing... ๐
I am emitting an event in one Component like this...
EventRegister.emit(Constants.EVENT_REDEEMED_COUPON, json)
I have registered a listener like this...
componentWillMount(){
this.listener = EventRegister.addEventListener(Constants.EVENT_REDEEMED_COUPON, (coupon) => {
this.setState({
coupon : coupon,
})
this.makeRemoteRequest();
})
}
When an event is emitted, the listener is never called.
How can I debug this?
Is this possible to send objects?
I don't want to have to manually emit
an event on every button
hi, as title,
could it work with the receiver in A.js, and sender in B.js?
thanks in advance
Integrated this library for emitting events on an action and on other screen added some listeners to listen for the same events, which is working pretty fine as expected on simulator and device during debugging. But after release builds are generated for iOS and Android devices, its not working at all.
Anyone else has faced same issue?
Thank you for this awesome module.
Whenever I perform removeEventListener
or removeAllListeners
after registering a listener it does remove from ref
array but count
property remains unchanged. Why is that?
In addition to that, if I emit an event after removing the listener it still invokes the handler. Anything I'm missing here?
In my case, the listener is called multiple times for each emit.
useEffect(() => {
const lst = EventRegister.addEventListener("nextprev", (data) => {
tr("EventRegister.addEventListener " + data);
if (data === "next") {
handleSkipToNext();
} else {
handleSkipToPrevious();
}
});
return () => {
EventRegister.removeAllListeners();
};
}, []);
I emit like this:
<IconButton
icon="skip-next"
onPress={() => {
EventRegister.emit("nextprev", "next");
size={iconSize}
/>
Did I miss something?
./node_modules/react-native-event-listeners/index.js 4:20
Module parse failed: Unexpected token (4:20)
File was processed with these loaders:
static _Listeners = {
| count: 0,
| refs: {}
I have a simple popup component that I call using the event listener but the event listener is being called twice, I get two popups one on top of each other and I see two logs in the console. See below my code.
My listener component:
`
export const PopupModal = (props) => {
const [isModalVisible, setModalVisible] = useState(false)
const toggleModal = () => {
setModalVisible(!isModalVisible);
};
const [msg, setMsg] = useState(() => {
return {
title: '',
buttonText: '',
message: ''
}
})
useEffect(() => {
const subscribe = EventRegister.addEventListener('showMsg', (data) => {
setMsg(data)
setModalVisible(true)
})
return () => {
EventRegister.removeEventListener(subscribe)
}
}, [])
return (
<>
{isModalVisible && <View style={{ flex: 1, position:'absolute' }}>
<Modal coverScreen={true} isVisible={isModalVisible} >
<View style={Styling.popupContainer}>
<Text style={Styling.popupTitle}>{msg.title}</Text>
<Text style={Styling.popupText}>{msg.message}</Text>
<View >
<TouchableOpacity style={Styling.popupTouchableOpacity} onPress={toggleModal} ><Text style={Styling.touchableOpacityTxt}>{msg.buttonText}</Text></TouchableOpacity>
</View>
</View>
</Modal>
</View>}
</>
);
}
`
its being triggered onPress like so
EventRegister.emit('showMsg', messagesService.popUpContactUsError)
Thanks in advance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.