4

Need to understand about the difference between these two func in ReactJs. ComponentWillMount(){} VS ComponentDidMount(){}

asked Oct 15, 2018 at 14:09
3
  • The former is invoked right before the component will mount to the DOM. The latter right after. This is suggested by their names... will and did. Commented Oct 15, 2018 at 14:11
  • Got it. Thank you very much. Commented Oct 15, 2018 at 14:20
  • 1
    Possible duplicate of What is the difference between componentWillMount and componentDidMount in ReactJS? Commented Oct 15, 2018 at 14:41

3 Answers 3

4

componentWillMount() gets called before first render whereas componentDidMount() gets called after first render. Both of these components gets called only once

Please note that componentWillMount() is deprecated in latest React version v16

answered Oct 15, 2018 at 14:12
Sign up to request clarification or add additional context in comments.

3 Comments

Constructer and ComponentWillMount are same or not?
constructor is basically to initialise state and do function bindings. componentWillMount does almost same but we don’t use this method much. Because we do all initialisations in constructor and this is recommended over componentWillMount
Ok. Got it. Thank you very much
1

Most discussed topic still explaining my understanding. ComponentWillMount executes before render as name suggest ComponentAfterMount executes before render once component gets rendered on screen as name suggest.

look at console logs in example

class App extends React.Component {
 componentWillMount(){
 console.log("console loging before component will mount");
 }
 componentDidMount(){
 console.log("console loging after mounting component");
 }
 render(){
 console.log("rendering component")
 return(
 <div> component</div>
 )
 }
}
ReactDOM.render(<App />, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root' />

answered Oct 15, 2018 at 14:24

3 Comments

You are saying that ComponentWillMount execute just before rendering component and ComponentDidMount execute just after rendering component. right?
Yes componentDidMount executes after component renders completely
Got it. Thank you very much
1

Difference

componentDidMount()

  • invoked immediately after a component is mounted.
  • it's gets called only once per mounting so good place for network requests and subscriptions.
  • do not assign state here or call setState.

componentWillMount()

  • invoked just before mounting occurs.
  • the only method which is called on server-rendering
  • should avoid using this ( it is a legacy method and will be removed in upcoming react version ).

You should avoid using componentWillMount and use componentDidMount and constructor instead Please check the official react docs for more info

answered Oct 15, 2018 at 14:34

1 Comment

Got it. Thank you very much.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.