0

I am trying to dispatch an action that removes an element from an array, I get the error message:

Warning: setState(...): Cannot update during an existing state transition (such as within render

   removeAchievement(achievement){
        this.props.removeAchievement({
           type: 'REMOVE_ACHIEVEMENT',
            id: this.props.day.id,
            text: achievement,
    })
  }

  render() {

    let listItems

    if(this.props.day.achievements) {
       listItems = this.props.day.achievements.map((achievement) => (
           <div className="cell" key={achievement + "_achievements"}>
             {achievement}
                 <div className="buttons">
                      <a href="#" onClick={this.openAchievementModal} className="edit">
                        <Icon name="pencil-square" size="3x" />
                      </a>
                      <a href="#" onClick={this.removeAchievement(achievement)} className="remove">
                        <Icon name="trash-o" size="3x" />
                      </a>
            </div>
           </div>
       ))
    }

How can I get the text into my action when I dispatch it?

1 Answer 1

2

You want to set the onClick handler to a function, not to result of calling the function:

          <a href="#" onClick={() => this.removeAchievement(achievement)} className="remove">
            <Icon name="trash-o" size="3x" />
          </a>
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.