0

I am trying to update a state based on my props in my react Class based component but i am getting an error

TypeError: Cannot assign to read only property 'statusKnown' of object '#'

here is the code below

componentDidMount(){
    this.calClass()
  }

calClass = () => {
    console.log(this.props.statusKnown, this.state.active);
    if (this.props.statusKnown == "deactive") this.setState({ active: false });
    else if ((this.props.statusKnown = "active")) this.setState({ active: true });
  };

and then in parent component

<Button item={item} categoryID={categoryID} statusKnown={status}/>;

If react doesn't allow this thing, then what is the possible solution for this ?

1
  • Could you accept my answer, if that was the issue? Commented Apr 9, 2020 at 21:20

1 Answer 1

1

You missed some = signs. You should always use ===

componentDidMount(){
    this.calClass()
}

calClass = () => {
    console.log(this.props.statusKnown, this.state.active);
    if (this.props.statusKnown === "deactive") this.setState({ active: false });
    else if ((this.props.statusKnown === "active")) this.setState({ active: true });
};
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.