Answer the question
In order to leave comments, you need to log in
Aren't componentWillMount and constructor the same thing?
In general, I write the Contact container and when initializing it, I want it to change the color of the header, I call the action in the constructor, everything seems to work, but in the chrome devTool there is an error:
Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
componentWillMount()
, but before that I read the following on Habré:
. . .
All but one of the bean's lifecycle methods can be defined, as one might expect, using the new class definition syntax. The class constructor now acts as the previously used componentWillMount method:// The ES5 way var EmbedModal = React.createClass({ componentWillMount: function() { … }, });
// The ES6+ way class EmbedModal extends React.Component { constructor(props) { super(props); // Operations usually carried out in componentWillMount go here } }
. . .
componentWillMount() is invoked immediately before mounting occurs. It is called before render(), therefore setting state in this method will not trigger a re-rendering. Avoid introducing any side-effects or subscriptions in this method.
This is the only lifecycle hook called on server rendering. Generally, we recommend using the constructor() instead.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question