Redux: componentWillMount is called twice.

Created on 20 Apr 2016  ·  1Comment  ·  Source: reduxjs/redux

LoginCompontent

  componentWillReceiveProps(nextProps) {
    const router = this.context.router;
    const { query } = this.props.location;
    if (!nextProps.success) {
      return;
    }
    switch (nextProps.data.code) {
      case 0:
        query.next ? router.push(query.next) : router.push('/');
        break;
      case 1:
        break;
      default:
        break;
    }
  }

CategoryCompontent

  componentWillMount() {
    const { params } = this.props;
    this.props.fetchCategories(params.id);
  }

Result
screen shot 2016-04-20 at 4 56 23 pm

Most helpful comment

Hi!

Redux is a library that contains one thing: an event emitter that calls your reducer function. It can’t possibly be responsible for calling your React components. So it is not a bug here, and StackOverflow might be a better place to ask about this.

The lifecycle books are managed by React. If you see that componentWillMount runs twice it means the component was unmounted between those calls for some reason. You can set a breakpoint in componentWillUnmount and check the stack trace to see what is causing the component to unmount.

I hope this helps!

>All comments

Hi!

Redux is a library that contains one thing: an event emitter that calls your reducer function. It can’t possibly be responsible for calling your React components. So it is not a bug here, and StackOverflow might be a better place to ask about this.

The lifecycle books are managed by React. If you see that componentWillMount runs twice it means the component was unmounted between those calls for some reason. You can set a breakpoint in componentWillUnmount and check the stack trace to see what is causing the component to unmount.

I hope this helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ramakay picture ramakay  ·  3Comments

timdorr picture timdorr  ·  3Comments

cloudfroster picture cloudfroster  ·  3Comments

caojinli picture caojinli  ·  3Comments

amorphius picture amorphius  ·  3Comments