gbland777
gbland777

Reputation: 725

React Native Event Emitter between components

I have two components in react native. One class name is firstClass, and the second class is secondClass. "secondClass" is a child view of firstClass. I can submit my data to my firebase from the child view, but on successful submit, I want the view to go back to "firstClass".

I have used event emitters in the past, but those were only using navigatorIOS.

My question is, does the event emitter system listen to global events, or just within their class? And if so, how do I communicate from "secondClass" to "firstClass", that I want to show "firstClass" again.

Thanks!

Upvotes: 1

Views: 3261

Answers (1)

LHIOUI
LHIOUI

Reputation: 3337

You can use a callback function and call it from the second component :

in the first component:

//Constructor 
   ....
   this.state ={
      renderSecondComponent:false
   }
   ....
...Your code 
{
   this.renderSecondComponent()
}

renderSecondComponent(){
   if(this.state.renderSecondComponent){
      return(
         <SecondComponent callback={()=>{this.setState({renderSecondComponent:false})}}/>
      )
   }
}

In the second component

....Your code 
// when you should go back to the first component 
if(this.props.callback){
  this.props.callback()
}

Upvotes: 1

Related Questions