andrea2010
andrea2010

Reputation: 338

setState(...) Can only update a mounted or mounting component

I have a page with registers, when I just go to it, they load if there are a lot of them, but if I go to the bottom of the page, they all disappear, and this error will reappear.

An error occurred in the console when the list of registers was received.

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the RegistersList component.

The supposed portion of the code to which the error goes.

  fetchRegistersOnScroll() {
    const { actions, current, dispatch } = this.props
    page++

    actions.fetchRegisters(current, page)
      .then(res => {
        dispatch({ type: 'REGISTER/SCROLL', payload: res.data });

        if (res.data.items.length < 20)
          this.setState({ hasMoreItems: false })
      })
  }

Upvotes: 0

Views: 275

Answers (1)

Archit Gupta
Archit Gupta

Reputation: 56

I think your component remounts again when your are trying to update the redux store. So I suggest you to setState before the dispatch action. Also, in the other link of the post where you posted the complete code, you are making two redux dispatch actions simultaneously. I don't know what are they suppose to do exactly but I think it would be better if you could make them in one single request.

if (res.data.items.length < 20)
    this.setState({ hasMoreItems: false }) 

Alternatively, you can call setState in the componentWillReceiveProps too. That can also help you to remove the error.

I hope it helps. Thanks!!

Upvotes: 1

Related Questions