Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How lifecycle methods in react Js are working with the variable unsubfromAuth?
class App extends Component{
constructor(){
super() 
this.state = {
  currentuser : null
}}
  
unsubfromAuth = null

componentDidMount(){

this.unsubfromAuth =  auth.onAuthStateChanged((user) => {
  this.setState({currentuser : user})
  console.log(user)
})
  }
 componentWillUnmount() {
    this.unsubfromAuth()
  }
render(){
  return (
    <div className="App">
      <Router>
      <Header currentUser={this.state.currentuser}/>
        <Switch>
          <Route exact path="/" component={Home}/>
          <Route exact path="/Hats" component={Hats}/>
          <Route exact path="/shoes" component={Shoes}/>
          <Route exact path="/jackets" component={Jacket}/>
          <Route exact path="/men" component={Men}/>
          <Route exact path="/women" component={Women}/>
          <Route exact path="/shop" component={Shop}/>
          <Route exact path="/signin" component={Signin}/>
        </Switch>
      </Router>      
    </div>
  )
}}

export default App

Why it is used in ComponentWillUnmount? I heard something about memory leak, does it prevents from it? why it was changed in ComponentDidMount? I'm using firebase in Reactjs.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is the standard flow in a React Component lifecycle, when you have to subscribe to some event. This is just the initialization of the instance variable, it is executed as soon as the Component is mounted:

unsubfromAuth = null

This is where you effectively subscribe to your event ("auth-state-changed") and start to listen to it, you want always to subscribe to events like this in componentDidMount or useEffect if you use hooks, since this is executed when the Component has fully mounted.

The method auth.onAuthStateChanged() returns a function that automatically clears any subscription when called.

componentDidMount(){

this.unsubfromAuth =  auth.onAuthStateChanged((user) => {
  this.setState({currentuser : user})
  console.log(user)
})
  }

The unsubscription method will be called in:

componentWillUnmount() {
    this.unsubfromAuth()
  }

This will be executed just before the Component is going to unmount to clear the subscription, since you don't want your event listeners, and subscriptions to keep being in memory when you unmount, or you will end up with memory leaks if your component mounts and unmounts many times.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!