Skip to main content

How to make cascading dropdowns in React JS

 We can make dropdowns in React JS by following method :

Step 1 : Take a list of states and cities object related to every state or we can get this data using apis.

Step 2 : Loop over the states data to show dropdown related to All States.

Step 3 : Now save the selected state.

Step 4 : Based on selected state value , get the corresponding cities and show them in dropdown as follows :

In App.js 

import { useState } from "react";
import './App.css';


function App() {
  const states = ['UP', 'Delhi', 'Gujrat']
  const cities = {
    'UP': ['f', 'g', 'l'],
    'Delhi': ['a', 'b'],
    'Gujrat': ['tr', 'trt', 'rtt'],
  }

  const [selectedState, setSelectedState] = useState('')
  console.log(selectedState)
  return (
    <div>
      CASCADING DROPDOWNS :
      <select onChange={(e) => { setSelectedState(e.target.value) }}>
        {
          states.map(state => {
            return <option>{state}</option>
          })
        }
      </select>

      {selectedState && <select>
        {
          cities[selectedState].map(city => {
            return <option>{city}</option>
          })
        }
      </select>}

    </div>
  );
}

export default App;

In App.css (optional)

select{
    width: 150px;
    margin : 20px;
}

 

Comments

Popular posts from this blog

Shopping Cart React.js Project with Explanation✅ [ Simple Ecommerce Website using Reactjs ]

Hello guys in this video we will discuss shopping cart web app.  first we need to install the basic react app using - npx create-react-app app-name The folder structure should be containing three files  in components folder as below : In App.js :  import './App.css' ; import Header from './components/Header' ; import ProductList from './components/ProductList' ; import CartList from './components/CartList' ; import { useState } from 'react' ; function App () {   const [ product , setProduct ] = useState ([     {       url : 'https://rukminim1.flixcart.com/image/300/300/l51d30w0/shoe/z/w/c/10-mrj1914-10-aadi-white-black-red-original-imagft9k9hydnfjp.jpeg?q=70' ,       name : 'TRQ White Shoes' ,       category : 'Shoes' ,       seller : 'AMZ Seller Ghz' ,       price : 1999     },     {       url : 'https://5.imimg.com/data5/KC/PC...

MERN Authentication - Login, Register, Verification Email, Forget Password, User Name & Logout on Navbar

In The React Side :  The folder structure will be : Lets start from App.js  On this file we will be adding routing , install the package react-router-dom : import './App.css' ; import React from 'react' ; import {   BrowserRouter as Router ,   Routes ,   Route ,   Link } from "react-router-dom" ; import Signup from './components/Signup' ; import Signin from './components/Signin' ; import Home from './components/Home' ; import ForgetPassword from './components/ForgetPassword' ; import NewSubmit from './components/NewSubmit' ; function App () {   return (     < div >       < Router >         < Routes >           < Route path = "/signup" element = { < Signup /> } />           < Route path = "/signin" element = { < Signin /> } />           < Route path = "/" e...

API Call In React JS using Axios [ React Form Submit To API ]

 Step 1 : npm i axios.  Step 2 : Create form with email and password.  Step 3 : create state to store email and password.  Step 4 : Store the value in state using onChange method.   Step 5 : Call the Api with data stored in state.   Step 6 : Handle Error and Success.   App.js Code below : import { useState } from "react" ; import './App.css' ; import axios from "axios" ; function App () {   const [ email , setEmail ] = useState ( '' )   const [ password , setPassword ] = useState ( '' )   console . log ({ email , password })   const handleEmail = ( e ) => {     setEmail ( e . target . value )   }   const handlePassword = ( e ) => {     setPassword ( e . target . value )   }   const handleApi = () => {     console . log ({ email , password })     axios . post ( 'https://reqres.in/api/login' , {       email : email...