H
H
Hazrat Hajikerimov2017-02-22 18:40:26
React
Hazrat Hajikerimov, 2017-02-22 18:40:26

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`.

( From the post, react recommends using componentWillMount .)
The problem occurs if I follow the url to the component, while if I refresh the page while on the container page, the error does not occur, the state changes as it should.
I overcame the error using the method componentWillMount(), but before that I read the following on Habré:
React on ES6+

. . .
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
  }
}

. . .

and on the react website they also do not recommend using componentWillMount:

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.

I'm a little confused, how in this situation, how to call an action, so that it would work and that it would comply with general conventions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2017-02-27
@davidnum95

call in componentDidMount()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question