Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
React.JS : make fetch to populate select

I'm trying to make a fetch to populate my select, but I can't use the fetch..

const loadOptions = async (searchQuery, loadedOptions, { page }) => {
    const requestMethod = {
                method: "PUT",
                headers: { 'Content-Type': 'application/json' },
                body: { realmIds: [props.realmScelto] }
            };
    
            const response = await fetch('myapi', requestMethod) // error is here, in requestMethod

            // const response = await axios.post(`myapi?page=${page}&size=20`, { realmIds: [props.realmScelto] }) // this works.. it gives me back 1 value, and it ok but add always the same value that I obtain from api

            // const optionToShow = await response.data
            const optionToShow = await response.json()
 return {
            options: optionToShow,
            hasMore: optionToShow.length >= 1,
            additional: {
                page: searchQuery ? 2 : page + 1,
            },
        };
    };

    const onChange = option => {
        if (typeof props.onChange === 'function') {
            props.onChange(option);
        }
    };

    return (
        <AsyncPaginate
            value={props.value}
            loadOptions={loadOptions}

            onChange={onChange}
            isSearchable={true}
            placeholder="Select"
            additional={{
                page: 0,
            }}
        />
    );

The error that I receive in the requestMethod is:

The argument of type '{method: string; headers: {'Content-Type': string; }; body: {realmIds: any []; }; } 'is not assignable to the parameter of type' RequestInit '. The types of the 'body' property are incompatible. The type '{realmIds: any []; } 'is not assignable to type' BodyInit '. In type '{realmIds: any []; } 'The following properties of type' URLSearchParams' are missing: append, delete, get, getAll and 4 more.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For fetch, the body you pass in is not converted to JSON automatically.

Try

const requestMethod = {
    method: "POST",
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ realmIds: [props.realmScelto] })
};
const response = await fetch(`myapi?page=${page}&size=20`, requestMethod)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda