Event Listener In React Native – Sending And Receiving Events Across Different Files

Recently I came across a case where I wanted to call 2nd API in file A once 1st API is called in file B. Here A file should know when 1st  API is called from file B. To achieve this communication between two files I used react-native-event-listeners. This library helped me in sending and receiving events across different files. Every event which occurs in React Native is either user or API generated.

React Native is a programming framework that uses a universal programming language called JavaScript. React Native gives developers the ability to develop full, native mobile applications for both Android and iOS. The main benefit of applying React Native is that developers won’t have to build the same Android and iOS. Instead, developers can again use the same coding across each platform, and it takes a shorter development time.

Event Listener is an important part of JavaScript where UI responds according to an event that occurs. Event listeners wait for an event to occur and pass that event across different files. It helps us to add interactive functionality to our react native app by listening to the events.

Let’s see an example of Event Listener in React Native hooks.

Installations:

npm install --save react-native-event-listeners

For ios,

cd ios && pod install

Import library in both the files A and B,

Import EventRegister from react-native-event-listeners. Add emit configs in any one of the files. I will export it in file A. You can have multiple configs for different events.

Import emitConfig in file B,

Let’s say we have called 1st API in file B using fetch. Fetching resources across the network are provided by fetch API. Fetch provides request and response objects. Once we get the success response we will emit the event like below,

Here with emit we are sending events using the emit method. Where the 1st param is eventName and the second one is data or message which we want to pass. Request to do something is known as emitting the event.

Syntax:

emit(eventName, data)

Now let’s switch to file A to receive the event and call 2nd API. To receive an event we will use the addEventListener() method inside useEffect to call it as soon as possible.

Syntax:

addEventListener(eventName, callBack)

As soon as we receive the event we will call the 2nd API inside the callBack. Once the event is used we will remove it using removeEventListener method. Waiting for an event or events to be processed is known as a subscriber.

coma

Conclusion

Using event listener in react native we have achieved communication between two files for sending and receiving events successfully. I hope this article will help you in various ways to handle events that may be user-generated or API generated.

Sayali Sonawane

Tech Expert
Sayali is a React Native Developer with around 2.5 years of experience in developing mobile applications. She is familiar with writing blogs for the last 3 years. She is self-motivated, a fast learner, and result-oriented. She loves to write blogs on technical issues faced by developers in day-to-day work and help them through her blogs.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks