0

I added a join button next to each group as follows:

var publicGroupItems = publicGroups.map(group =>
            <ListItem key={group._id} global href={'/groups/' + group._id} image={group.imageUrl} title={group.name} ><button type="button" onClick={this.joinGroup(group._id)}>joinGroup</button></ListItem>)

When I click on the button it goes through each of the groups ids and adds the current user to ALL groups.

How can I make it so that the user is only added to the specific group where I clicked join?

1
  • Welcome to StackOverflow! In the future when posting questions, please make sure to properly format your code using the { } code formatting option. Commented May 13, 2015 at 14:35

1 Answer 1

2

The problem here is your onClick

onClick={this.joinGroup(group._id)}

Since you have this.joinGroup(group._id) as a function call, it will be called once for each group before the button is even clicked.

You need to reference your function instead of calling it:

onClick={this.joinGroup.bind(this, group._id)}

This will trigger joinGroup when the button is clicked. The first argument will be the value of group._id and the second will be the SyntheticEvent.

joinGroup: function(groupId, event) {
  //Join The Group
}
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.