1

I need to make a form to submit one of several choices and i need to use react-redux so i cannot use local state to change the choice selected

the app does work (which means componentdidmount is working) but if a select one of the choices i get TypeError: Cannot read property 'props' of undefined

componentDidMount() {
    this.props.setModelSelector(data)
  }

  handleChange(event) {
   this.props.selectCar(event.target.value)//the function doesn't get called
    //this.props.setModelSelector(data)
    event.preventDefault();
  }

handleChange(event) {
   //this.props.selectCar(event.target.value)
    this.props.setModelSelector(data)//Even if i run the same function of componentDidMount i get the same error
    event.preventDefault();
  }

I have also checked event.target.value contains indeed the string that i need to change the value showed in the form

TypeError: Cannot read property 'props' of undefined
 handleChange
src/components/ModelDetailsContainer.js:40
   37 | 
    38 | handleChange(event) {
   39 |  // this.props.selectCar(event.target.value)
 > 40 |   this.props.setModelSelector(data)
      | ^  41 |   event.preventDefault();
    42 | }
   43 |
2
  • @RandyCasburn i don't think it's a duplicate because in none of the answers it tells you to use arrow functions, which was the solution to my problem. Commented Jun 24, 2019 at 8:01
  • Arrow functions are an alternative because they don't bind the this keyword. either solution (binding or arrow functioin) will work. And now you know :-) Commented Jun 24, 2019 at 13:25

1 Answer 1

1

this is getting rebound in your event handler. Either change handleChange(event) { to handleChange = event => {, or do this.handleChange = this.handleChange.bind(this); in your constructor.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.