In this example, i will show you react radio button onchange event example. if you want to see example of react radio buttons example then you are a right place. we will help you to give example of react radio button group example. it's simple example of how to implement radio button in react.
If you are new in react js then you want to see how to use radio button in react app. but it's very easy to use radio button 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 "gender" with male and female radio button 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 = {
gender: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({gender: event.target.value});
}
handleSubmit(event) {
console.log(this.state);
event.preventDefault();
}
render() {
return (
<div>
<h1>React Radio Buttons onChange Example - HDTuto.com</h1>
<form onSubmit={this.handleSubmit}>
<strong>Select Gender:</strong>
<label>
<input
type="radio"
value="male"
onChange={this.handleChange}
/>Male
</label>
<label>
<input
type="radio"
value="female"
onChange={this.handleChange}
/>Female
</label>
<input type="submit" value="Submit" />
</form>
</div>
);
}
}
render(<App />, document.getElementById('root'));
Output:
{gender: "male"}
I hope it can help you...
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example