monsterpiece
monsterpiece

Reputation: 779

react function undefined when passed to functional component as a prop

I am passing a function defined in my main app class to a functional component that accepts props. For some reason I'm getting the error that the function is undefined. Can someone help me out? I'ts probably super obvious but I can't think of the reason for the error.

//inside APP.js

updateProgress = (val) =>{
    this.setState({progress:val,currentid:val-1}).then(()=>{
      console.log("progress",this.state.progress,"progress",this.state.currentid)
    })
  }

<mycomponent updateProgress={this.updateProgress} mainprops={this.state}/>

// inside functional component script

const cooloptions = props => {
  return (

    props.options.map(o => (
      <div key={o.key}>
        <label htmlFor={o.key}>
            <strong>{o.title}</strong>
            <p>{o.description}</p>
        </label>
        <input id={o.key} name={o.name} onClick={updateProgress(props.mainprops.progress+1)} type="radio" />
      </div>
    ))
  )
}

Upvotes: 2

Views: 3382

Answers (1)

Change

onClick={updateProgress(props.mainprops.progress+1)}

to

onClick={()=>props.updateProgress(props.mainprops.progress+1)}

Upvotes: 2

Related Questions