0

I have a function for set my states but it doesn't work correctly.

this is my function,

checkAnswer() {
  this.setState({
    isDropdownShown: false
  });
}

and i'm calling like this,

renderAnswers(answer, idx) {
  return (
    <li
      key={idx}
      className={styles.questionDropdownItem}
      onClick={() => this.checkAnswer(answer)}
    >
      {answer.title}
    </li>
  );
}

From His Comment:

I'm calling renderAnswers method in render like this:

{question.answers.map(this.renderAnswers)}

I'm try everything like shouldComponentUpdate but it doesn't work.

Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
Ugurcan Omur
  • 73
  • 2
  • 7

1 Answers1

1
{question.answers.map(this.renderAnswers)}

should be

{question.answers.map(this.renderAnswers, this)}

or

{question.answers.map((answer, i) => this.renderAnswers(answer, i))}

Your first example will call the renderAnswers function with no this value because.maphas no way to know whatthis` you'd want.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251