2

Before anyone marks this as a duplicate, I went through this question ReactJS Warning: Thumbnails.shouldComponentUpdate(): Returned undefined instead of a boolean value. Make sure to return true or false and it doesn't answer my question (or I wasn't able to get it (to say the least)

so this my code

  shouldComponentUpdate(nextProps, nextState) {
    console.log(this.props.order, nextProps.order)
    if (nextProps.order !== this.props.order) {
      return true;
    }
  }

Here when I check the console log in chrome, it throws a warning saying

shouldComponentUpdate(): Returned undefined instead of a boolean value. Make sure to return true or false

But everything works as expected (or the way I want to), Also when I do something like

shouldComponentUpdate(nextProps, nextState) {
    console.log(this.props.order, nextProps.order)
      return nextProps.order !== this.props.order
  }

It doesn't throw an error.

[Question] Why am i getting that warning? when I clearly return true?

1
  • add else return false after if, you only returned true Commented Jun 14, 2018 at 11:20

3 Answers 3

8

You are getting an error in the first case because you are not returning anything if (nextProps.order == this.props.order). so, you could have done this:

  shouldComponentUpdate(nextProps, nextState) {
    console.log(this.props.order, nextProps.order)
    if (nextProps.order !== this.props.order) {
      return true;
    }
    return false; //this is the missing piece
  } 
Sign up to request clarification or add additional context in comments.

Comments

1

In second variant it will definitely return true or false. But in your first piece of code, you don't specify else and it can be true or undefined, so you get that warning

Comments

0

When nextProps.order !== this.props.order is false the shouldComponentUpdate function dont enter in the if with return and will return undefined.

shouldComponentUpdate only want to return boolean values.

Try with your second impmenentation to return true or false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.