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

319
Visualizações
React Select - Not loading data from an API

I'm trying to load data into dropbox(Combobox) from an API. Below are 2 React-Select dropdowns but it doesn't display(Show) any data in dropbox. I Also tried to loop through in the if below the let option but got nothing.

I tried Using reast_select Axios but had more errors than before. If I console log I get the data but however, it's just blank.

import React from 'react'
import Select from 'react-select'
import {useEffect,useState} from 'react'
  
const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]


const ClientDropdown = () => {

  const [clientList,setClientList] = useState([])
  //console.log(clientList)

  useEffect(() => {
    clientData();
  },[])

  const clientData = async() => {
    const response = await fetch("https://localhost:7168/TestData");
    //console.log(response);
    const clientDataJson= await response.json();
    //console.log(clientDataJson);
    setClientList(clientDataJson);
  }

  let option = []
  if (clientList.values.length > 0) {
    clientList.values.forEach(client => {
      let roleDate = {}
      roleDate.value = client.studentid
      roleDate.label = client.sudentName
      option.push(roleDate)
      console.log('Im Here' + roleDate)
    })
  }
 
    return (
    <div>
     <Select options={option} />

     <Select
     {
          ...clientList.map((val) => {
          return (
            <options vlaue={val.studentid} label={val.sudentName}></options> 
          )
        })
      }
    />
    </div>
    )
}
export default ClientDropdown

dropdown

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You used clientData for both function name and variable name inside of that function. Also You are using spread operator when you use map like ...clientList.map((val). but you should use clientList.map((val) instead of ...clientList.map((val). I made correction in the below code. please check it.

import React from 'react'
import Select from 'react-select'
import {useEffect,useState} from 'react'
  
const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]


const ClientDropdown = () => {

  const [clientList,setClientList] = useState([])
  //console.log(clientList)

  useEffect(() => {
    clientData();
  },[])

  const clientData = async() => {
    const response = await fetch("https://localhost:7168/TestData");
    //console.log(response);
    const data = await response.json();
    //console.log(data);
    setClientList(data);
  }

  let option = []
  if (clientList.values.length > 0) {
    clientList.values.forEach(client => {
      let roleDate = {}
      roleDate.value = client.studentid
      roleDate.label = client.sudentName
      option.push(roleDate)
      console.log('Im Here' + roleDate)
    })
  }
 
    return (
    <div>
     <Select options={option} />

     <Select
     {
          clientList.map((val) => {
          return (
            <options vlaue={val.studentid} label={val.sudentName}></options> 
          )
        })
      }
    />
    </div>
    )
}
export default ClientDropdown
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