Introduction

In this article we are going to learn how we use radio button and dropdown select in ReactJS. Radio button is a element that allows the user to select only one option from multiple options.
You can check my previous articles on Reactjs from the below mentioned links.
Prerequisites
  • We should have basic knowledge of React.js
  • Visual Studio Code IDE
  • Bootstrap

Create React.js Project

Lets create a new React.js project by using the following command.
  1. npx create-react-app formelement
Open the newly created project in Visual Studio Code and install Bootstrap in this project by using the following command.
  1. npm install --save bootstrap
Now, open the index.js file and add Bootstrap reference.
  1. import 'bootstrap/dist/css/bootstrap.min.css';
Now, right click on "src" folder and add a new component named 'Form.js.
In reactjs component, to auto generate code, we need to install react Snippets in Visual Studio code. React Snippets are found in the Visual Studio Code Extension Marketplace. Download React snippets using the following link or go to VS Code Extension and search 'React Snippets'
Now open Form component and type 'rce' to create a class component class and press the tab key, it generates code for this component. Now create a constructor by using 'rconst' snippets and add two state variables.
  1. constructor(props) {
  2. super(props)
  3. this.state = {
  4. Technology: '',
  5. Gender: ''
  6. }
  7. }
Now add two event handlers, one for radio button click and another from dropdwon select event.
  1. Changetechnology = (e) => {
  2. this.setState({
  3. Technology: e.target.value
  4. })
  5. }
  6. Changegender = (e) => {
  7. this.setState({
  8. Gender: e.target.value
  9. })
  10. }
Add one more event handler for button click. When clicking on button this event is called.
  1. onsubmit = (e) => {
  2. e.preventDefault();
  3. alert(`${this.state.Technology},${this.state.Gender}`)
  4. }
Now in render method which returns a dropdown add two radio buttons.
  1. render() {
  2. return (
  3. <div>
  4. <div class="row" className="hdr">
  5. <div class="col-sm-12 btn btn-info">
  6. Radio button and Dropdowns in React
  7. </div>
  8. </div>
  9. <form onSubmit={this.onsubmit}>
  10. <div className="form-group dropdn">
  11. <select className="form-control" value={this.state.Technology} onChange={this.Changetechnology} >
  12. <option value="1">Angular</option>
  13. <option>React</option>
  14. <option>JavaSript</option>
  15. <option>Vue</option>
  16. </select>
  17. </div>
  18. <div>
  19. <input type="radio" value="Male" checked={this.state.Gender == "Male"} onChange={this.Changegender} />Male
  20. <input type="radio" value="Female" checked={this.state.Gender == "Female"} onChange={this.Changegender} />Female
  21. </div><br></br>
  22. <button type="submit" className="btn btn-info" >Click</button>
  23. </form>
  24. </div>
  25. )
  26. }
Complete code
  1. import React, { Component } from 'react'
  2. import './App.css'
  3. export class Form extends Component {
  4. constructor(props) {
  5. super(props)
  6. this.state = {
  7. Technology: '',
  8. Gender: ''
  9. }
  10. }
  11. Changetechnology = (e) => {
  12. this.setState({
  13. Technology: e.target.value
  14. })
  15. }
  16. Changegender = (e) => {
  17. this.setState({
  18. Gender: e.target.value
  19. })
  20. }
  21. onsubmit = (e) => {
  22. e.preventDefault();
  23. alert(`${this.state.Technology},${this.state.Gender}`)
  24. }
  25. render() {
  26. return (
  27. <div>
  28. <div class="row" className="hdr">
  29. <div class="col-sm-12 btn btn-info">
  30. Radio button and Dropdowns in React
  31. </div>
  32. </div>
  33. <form onSubmit={this.onsubmit}>
  34. <div className="form-group dropdn">
  35. <select className="form-control" value={this.state.Technology} onChange={this.Changetechnology} >
  36. <option>Angular</option>
  37. <option>React</option>
  38. <option>JavaSript</option>
  39. <option>Vue</option>
  40. </select>
  41. </div>
  42. <div>
  43. <input type="radio" value="Male" checked={this.state.Gender == "Male"} onChange={this.Changegender} />Male
  44. <input type="radio" value="Female" checked={this.state.Gender == "Female"} onChange={this.Changegender} />Female
  45. </div><br></br>
  46. <button type="submit" className="btn btn-info" >Click</button>
  47. </form>
  48. </div>
  49. )
  50. }
  51. }
  52. export default Form
Open app.js file and add the following code in this file,
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import Form from './Form';
  5. function App() {
  6. return (
  7. <div className="App">
  8. <Form></Form>
  9. </div>
  10. );
  11. }
  12. export default App;
Now run the project by using 'npm start' command,
How To Use Dropdown And Radio Buttons In React
Select a value from dropdwon and check a radio button and click on button and check
How To Use Dropdown And Radio Buttons In React

Summary

In this article we learned how we use dropdown and radio button in React.js applications. We also checked how we add React snippets in Visual Studio code. In the next article we will discuss all available snippets. By using these snippets we can save time in development.