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

348
Visualizações
Material ui load select option data list from server response but when select a option can not return selected value

I'm trying to use server response data to create my country and province select component. first, the user chooses the country. Then we send a request to the server with the selected country id, get the country's provinces, and then re-render the province's select component with new data. finally, I submit the form but show the error country is a required field. I'm adding to the country select component (onChange) and saving the selected country id in the state but it does not solve this issue. below is my select option component code:

<RHFSelect value={selects.country} name="country" label="Country" placeholder="Country" onChange={(e) => handleCountrySelect(e.target.value)}>
    <option value="" />
    {countries.map((option) => (
       <option key={option.id} value={option.id}>
           {option.name}
       </option>
    ))}
</RHFSelect>

my state

const [selects, setSelects] = useState({
    country: undefined,
    province: undefined
});

on change handler function

const handleCountrySelect = (id) => {
    setSelects((props) => ({ ...props, country: id }));
    getProvince(id);
};

I'm using minimal dashboard source code and it's for handling the form using react-hook-form

const methods = useForm({
   resolver: yupResolver(UpdateUserSchema),
   defaultValues,
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use react-select if possible. https://react-select.com/home and here is the example how to use

<Select closeMenuOnSelect={false}
 isMulti as="select"
 options={options} 
 value={options.filter(obj => 
 selectedValue.includes(obj.value))} // set selected values
 onChange={handleChange}/>
const [selectedValue, setSelectedValue] = useState([]);

  const handleChange = (e) => {
    setSelectedValue(Array.isArray(e) ? e.map(x => x.value) : []);
  }

console.log(selectedValue)
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