0

Now I check by using 'componentWillRecieveProps' to check 'nextProps' of Redux state is it finish or not.

like this example,

if (!nextProps.a.update && !nextProps.a.error &&
  nextProps.a.update !== this.props.a.update) {
  const oldData = [...this.props.a.data];
  const newData = [...nextProps.a.data];
        .
        .
        .
  });
}
}

Did anyone have another good idea to check it?

Thanks.

Vikas Yadav
  • 3,094
  • 2
  • 20
  • 21
ppppp
  • 199
  • 1
  • 2
  • 15

2 Answers2

0

if you are using any middleware like Redux-saga or thunk you can check whether update is done or not from the middleware itself and once its updated you can dispatch an action from middleware itself and get notified. Not sure how much it will be appropriate for your case.

Roopak Puthenveettil
  • 1,387
  • 2
  • 13
  • 27
0

It really depends. 1. If the logic your'e trying to implement, is only for same component, and from one dispatched action only at a time, then using a flag is definitely OK, set true on 'will update' and false on 'did update' (same for mount if necessary). 2. If the logic is upon few actions, then maybe create a bulking mechanism using promise will be better. Again, depends on your logic. Maybe it will be enough to use flags for each type of update. 3. If the logic is system wide, i would use a global variable, maybe even set on 'window'. For example, i wanted to implement a variable called 'isLoading', which will define if there's any pending ajax call.

Please let me know if that's what you've been looking for.

Ben Cohen
  • 1,380
  • 1
  • 9
  • 12
  • what is the flag? And 'will update' can't setState(can't set flag). – ppppp Dec 21 '17 at 08:01
  • A flag - just a boolean indicator on your component. 1. will update can set state, just not with using setState but with updating the parameter nextState that you get. 2. You don't need to keep this flag on your state. you can use it on the component itself. The data update itself will cause update cycle, therefore this flag will always be triggered. – Ben Cohen Dec 21 '17 at 09:00