S
S
Sergey2020-06-25 23:51:46
React
Sergey, 2020-06-25 23:51:46

At what point in the lifecycle should the state be updated?

Good afternoon. The task on React is to implement the following logic:
In state, a defined field is null by default. When the page is loaded, the function is launched and checks if it is null, then it creates a field, if it has already been created, it returns it. For placing such a function in the render method, react swears, because I use setState in render. I assume that state needs to be filled before rendering, in a certain life cycle. Please tell me which one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-06-26
@hzzzzl

componentDidMount() {  // "При загрузке страницы запускается функция" это здесь
  if (something) {
    this.setState({ something: 'else' })
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question