ItSolutionStuff.com

React Checkbox onchange | React Checkbox Example

By Hardik Savani • September 6, 2020
React JS

In this tutorial we will go over the demonstration of react checkbox onchange event example. i explained simply step by step checkbox onchange event in react js. I’m going to show you about react js checkbox example. i explained simply about checkbox in react js example.

If you are new in react js then you want to see how to use checkbox in react app. but it's very easy to use checkbox input in react js app. you can use it as you use in html and you have to write change event on it. using that change event you have to store value into form state. so you can get that data on submit.

In this example, we will take simple "i_agree" with checkbox input and add onchange event with handleChange() then we will assign value on state variable array. Then on submit event we will take that values with state variable.

So, let's see bellow preview and code:

Example Code:

import React, { Component } from 'react';

import { render } from 'react-dom';

class App extends Component {

constructor() {

super();

this.state = {

i_agree: false

};

this.handleChange = this.handleChange.bind(this);

this.handleSubmit = this.handleSubmit.bind(this);

}

handleChange(event) {

this.setState({i_agree: !this.state.i_agree});

}

handleSubmit(event) {

console.log(this.state);

event.preventDefault();

}

render() {

return (

<div>

<h1>React Checkbox onChange Example - ItSolutionStuff.com</h1>

<form onSubmit={this.handleSubmit}>

<label>

<input

type="checkbox"

defaultChecked={this.state.i_agree}

onChange={this.handleChange}

/> I Agree with this content...

</label>

<input type="submit" value="Submit" />

</form>

</div>

);

}

}

render(<App />, document.getElementById('root'));

Output:

{i_agree: true}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

React Select Dropdown onchange | React Select Box Example

Read Now →
ā˜…

React Textarea onChange Example | React Textarea Tutorial

Read Now →
ā˜…

React Textbox onChange Example | React Text Input Tutorial

Read Now →
ā˜…

How to Change Date Format in React?

Read Now →
ā˜…

How to Get Current Date and Time in React?

Read Now →
ā˜…

How to Create Custom Component in React?

Read Now →
ā˜…

React Conditional Statements in Render Tutorial

Read Now →
ā˜…

React Switch Case Statement Example

Read Now →
ā˜…

React Bootstrap Tabs Example Tutorial

Read Now →