1

I am trying to validate the answers from inside the parent component. The validateAnswer function gets triggered and almost works as it should.

But I want to change the site if it was validated. So I call this.props.history.push from the parent. And this gives me an error because history is undefined.

<Answers handleClick={e => this.validateAnswer(e)} />

//Function to validate the Answer in the parent
    validateAnswer = (event) => {
    if (true) {
      //navigate to /email
      this.props.history.push({
        pathname: '/email',
        state: { lastPage: 'question' },
      });

    //Child component
         <Button
          name="1"
          label="Afrika"
          handleClick={props.handleClick}
        />

How could I fix this? Should I validate the answer directly in the Answer component?

Fabio ha
  • 503
  • 1
  • 8
  • 25

1 Answers1

0

Try this:

<Answers handleClick={this.validateAnswer} />

// Function to validate the Answer in the parent
validateAnswer(event){
if (true) {
  //navigate to /email
  this.props.history.push({
    pathname: '/email',
    state: { lastPage: 'question' },
  });

// Child component
 <Button
  name="1"
  label="Afrika"
  handleClick={(e) => props.handleClick(e) }
/>
Stretch0
  • 5,526
  • 3
  • 48
  • 93