jose azevedo
jose azevedo

Reputation: 245

Passing a parameter using React

I have this Json variable:

this.state = {
            groups: [{
                name: '',
                permission: [{
                    name: ''
                }]
            }]
        }

And on my render method i have this:

 render() {

        return (
            <div>
                <div>
                    <HeaderApp />

                    <h3>Choose a group to add permissions:</h3>

                    {this.state.groups.map((groups) => {
                        return (<li key={groups.code}>
                            <Card>
                                <CardBody>
                                    <CardTitle>{groups.name}</CardTitle>
                                    <CardSubtitle>This group has {this.countPermissionPerGroup(groups)} permissions.</CardSubtitle> 
                                    <Button color="secondary" onClick={(groups) => this.onClickShowPermissions(groups)}>Show permissions</Button>    
                                    <Button color="secondary">Add Permission</Button>
                                </CardBody>
                            </Card>
                        </li>)

                    })}


                </div>
            </div>
        );
    }

On the onClick method i want to pass the group where the button belongs and get all the permissions of that same group and print it on my console with this method:

onClickShowPermissions = (group) => {
    for(var g in group.permission){
        console.log("Test"+g)
    }

}

But for some reason that doesnt work,what am i doing wrong?

Upvotes: 0

Views: 40

Answers (1)

Taavi Kivimaa
Taavi Kivimaa

Reputation: 252

The onClick action has event type binded to it. So instead of trying to pass groups directly from onClick, leave it empty , like this:

<Button color="secondary" onClick={() => this.onClickShowPermissions(groups)}>Show permissions</Button>    

Upvotes: 1

Related Questions