1

I am doing a conditional in my shouldComponentUpdate method and if the condition matches return true, else return false... however I am still getting a "returned undefined instead of a boolean ..." as the title suggests.

Here is my method: * Note if I don't have the setTimeout it automatically assumes that nextProps.props.length is 0 even when its more than 0

shouldComponentUpdate(nextProps){
    setTimeout(()=>{
        console.log(nextProps.props, nextProps.props.length);
        if(nextProps.props.length > 0){
            return true;
        }else{
            return false;
        }
},1000)
}
4
  • why you have used setTimeout ? Commented Jul 27, 2017 at 13:00
  • what you are trying to achieve? Commented Jul 27, 2017 at 13:06
  • it was returning as length 0 without the setTimeout regardless if the length was actually 0. Saw that in another solution. I figured it out though, I just had to use this.state in shouldcomponentupdate not this.props or nextProps ... So this question is solved ... now another problem though Commented Jul 27, 2017 at 13:15
  • another problem ? Commented Jul 27, 2017 at 13:18

1 Answer 1

1

Your components props are actually stored in nextProps argument. It seems you're trying to access props in nextProps, which obviously looks fishy.

You should never name a prop of a component as generic as props, if that's really what you're trying to achieve. Give your component's props more descriptive names.

Also, the expression in shouldComponentUpdate can be simplified to:

shouldComponentUpdate(nextProps){
    return nextProps.props.length > 0; // assuming nextProps.props is an array
}

Additionally, you should post your component's propTypes definition, so the component's props contract is clear.

Sign up to request clarification or add additional context in comments.

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.