I saw this line as an answer to another question on here:
"componentWillMount should be componentDidMount, or else you'll leak event emitters in node."
and I don't really understand it. Can someone explain with more detail?
More info:
Building a react application with flux, as part of the initial render, a child component computes some data. Ideally, after this data is computed, I would like to call an action that updates the store's state with a portion of this new data.
Normally, updating the store's state emits a change event that causes a re-render. However, because the change listener isn't being added until componentDidMount (rather than in componentWillMount), my top level component isn't able to listen for the change that occurs during the initial render and initiate a re-render.
If I move the addChangeListener to componentWillMount that would seem to fix this issue, but the above quote suggests that this is a bad idea?
Is hard to understand what that quote means without more context. What I can tell you is that there are huge differences between the two of those methods.
On one hand,
componentWillMount
is called before the component is actually added to the DOM. This is the last chance you have to update component's state and get it rendered before the component is rendered by the browser.On the other hand,
componentDidMount
is called once the component has been attached to the DOM (the real one).What you need really depends on your use case. In general,
componentDidMount
is used to integrate with other libraries (like jQuery), it provides a way to modify the HTML rendered by the component.I suggest you to read these links: