I
I
Ilya2019-12-10 20:12:51
React
Ilya, 2019-12-10 20:12:51

How to create a data state listener?

Good afternoon.
Faced such task.
There is a global array in which it finds an object. The number of this object, that is, its index , is sent to the state as soon as I receive it.
Start state:

this.state = {
      index: 0,
      data: []
    };

After receiving:
this.state = {
      index: 1,
      data: []
    };

The task is this. As soon as index changes from 0 to 1, I need to send a request through redux to get the next object from the global array. And so on. As soon as state.index = 2 , I need to send a request to get the 3rd object.
How to do it ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2019-12-10
@GreyCrew

componentDidUpdate() is fine for this.

componentDidUpdate(prevProps, prevState){
  if(prevState.index !== this.state.index){ // проверяем, что обновился именно нужный нам state
    this.props.getNewObject() 
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question