Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
Why i am not getting dropdown in react js can any one solve me this

I want to get a drop down but i am not getting it. The api is being called but its not being diaplayed on dropdown. I an new to react can any one rewrite the code for me.

import React,{useState} from 'react';
    import axios from 'axios';
    
function App() {
    const handleSelect=()=> {
        axios
        .get("https://cdndemo-api.co-vin.in/api/v2/admin/location/states")
        .then((response) => {
          console.log(response.data);
          setState(response.data);
     });
    }
    const [state, setState] = useState([]);
  return <div>
      <select  onChange={handleSelect}
      >
        click
        <option value="">Choose a user</option>

      {state?.states?.map((value) => {
        return (
          <div>
            <option value={value.state_name} key={value.state_id}>{value.state_name}</option>
          </div>
        );
      })}
       </select>
  </div>;
}

export default App
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's better to keep your state's data type equal to the data type of the API response.

If the data you get from the API is an array then you can do something like this:

import React,{useState, useEffect} from 'react';
    import axios from 'axios';
    
function App() {
     //set initial state as []
    const [state, setState] = useState([]);
  useEffect(() => {
        axios
        .get("https://cdndemo-api.co-vin.in/api/v2/admin/location/states")
        .then((response) => {
          console.log(response.data);
          // if response.data is an type of an array use this
          setState(response.data);
     });
  }, []);

  return <div>
      <select
      >
        click
        <option value="">Choose a user</option>

       {/* Your mapping */}

      {state.length > 0 && state.map((value) => {
        return (
            <option value={value.state_name} key={value.state_id}>{value.state_name}</option>
    
        );
      })}
       </select>
  </div>;
}

export default App

If your data type is an Object then you can do something like the

    import React,{useState, useEffect} from 'react';
    import axios from 'axios';
function App() {
    //set initial state as null
    const [state, setState] = useState(null);
  useEffect(() => {
        axios
        .get("https://cdndemo-api.co-vin.in/api/v2/admin/location/states")
        .then((response) => {
          console.log(response.data);
          // if response.data is an type of an object use this
          setState(response.data);
     });
  }, []);

  return <div>
      <select
      >
        click
        <option value="">Choose a user</option>

       {/* Your mapping */}

      {state && state.states.map((value) => {
        return (
         
            <option value={value.state_name} key={value.state_id}>{value.state_name}</option>
          
        );
      })}
       </select>
  </div>;
}

export default App
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this code

import "./styles.css";
import axios from "axios";
import { useEffect, useState } from "react";

export default function App() {
  const [state, setState] = useState([]);

  useEffect(() => {
    axios.get("https://jsonplaceholder.typicode.com/todos").then((res) => {
      setState(res.data);
    });
  }, []);

  return (
    <div className="App">
      <select>
        {state.map((item) => (
          <option value={item.name} key={item.id}>
            {item.title}
          </option>
        ))}
      </select>
    </div>
  );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda