Neeraj Verma
Neeraj Verma

Reputation: 2294

React Checkbox not getting value onCheck

React Checkbox not getting value onCheck . I tried an accepted answer, and get constant value first , but it is getting undefined(data variable)

<TableCell>
    <Checkbox name="myCheckBox1" onChange={(e, data) => this.checkchange1('2', data.value)}/>
</TableCell>


checkchange1 = (e, data) => {
        console.log('it works');
        console.log(data.value);
    }

what am I missing ?

Upvotes: 1

Views: 74

Answers (2)

0000
0000

Reputation: 26

onChange will take only one param and that is e.

you can store the value in state like this.

<Checkbox onChange={this.toggle} checked={this.state.checked} /> 

 toggle = () => this.setState(({checked}) => ({ checked: !checked }))

Upvotes: 0

Gangadhar Gandi
Gangadhar Gandi

Reputation: 2252

To get the value of checkbox, Try like below

<Checkbox name="myCheckBox1" value="checkedA" onChange={(e) => this.checkchange1(e)}/>
checkchange1 = (e) => {
    console.log(e.target.value); /* checkedA will be consoled here */
}

Upvotes: 1

Related Questions