Pablo Pablo - 14 days ago 13
Javascript Question

React and Redux - ComponentDidUpdate Async?

I'm learning React and Redux, I'm doing a basic ToDo list.

I'm having an issue with the

componentDidUpdate
.

I delete a row over a
dispatch
to a redux action, and then in the componentDidUpdate I wait for the change in the props like:

componentDidUpdate = () => {
if (this.props.todoDelted) {
this.refreshTodoList()
}
}


The problem is, that there's another
props
that are being updated too, and
todoDelted
is the first to update. When I compare with
prevProps
todoDeleted is always
undefined
, since it updated just once.

How can I run
this.refreshTodoList()
just once and not every time a prop is updated?

Answer

Actually it's not necessary to use componentDidUpdate method.

The dispatch method returns Promise, so you can write something like this:

this.props.dispatch(someAction(payload)).then(this.refreshTodoList);

Your function will be called just once.

Comments