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

171
Visualizações
In react How to split the handle change value in react

I need to get the array of handle change value and pass into the API URL. I'll share my code.

import React from 'react';
import Select from 'react-select';

const Selects = [
    {
        name: 'firstSelect',
        options: [
            { value: 1, label: 'Vg' },
            { value: 2, label: 'sri' }
        ]
    },
    {
        name: 'secondSelect',
        options: [
            { value: 1, label: 'akila' },
            { value: 2, label: 'selvi' },
            { value: 3, label: 'shanmuga' }
        ]
    }
];

export default class Demo extends React.Component {
    onSelectChange(name, value) {
        let obj = [];
        obj[name] = value;
        this.setState(obj);

        console.log(obj[name].value);

        let url =
            'http://localhost:99999/api/GetProfile/Get_MyPostDetails?id=3&Year=' +
            obj[name].value +
            '&CategoryID=' +
            obj[name].value;

        let user = JSON.parse(localStorage.getItem('user'));
        const accessToken = user;
        console.log(accessToken);
        //console.log("hi");
        fetch(url, {
            method: 'GET',
            headers: {
                'Content-type': 'application/json',
                Accept: 'application/json',
                Authorization: 'Bearer ' + accessToken,
                'Access-Control-Allow-Headers': 'Access-Control-Request-Headers '
            }
            //body:JSON.stringify(data)
        })
            .then(response => response.json())
            .then(data => {
                this.setState({
                    like: data
                });
                console.log('Filter', data);
                // console.log(emps.profile_dateOfAnniversary);
            });
    }

    render() {
        return (
            <div>
                {Selects.map((select, i) => {
                    return (
                        <Select
                            key={i}
                            name={select.name}
                            options={select.options}
                            onChange={this.onSelectChange.bind(this, select.name)}
                        />
                    );
                })}
            </div>
        );
    }
}

When I select the First dropdown value it passes into the Year and Category Id also. I need to select the first dropdown value pass into the year and the second value is set into the CategoryId. Please share your Idea.

Thanks in Advance.

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

0

this.setState is asynchronous

When your use state in API Url first time, your state is empty yet. When you do it second time state have data from first this.setState call. Your must do API call in seState callback:

this.setState(
      (prev) => {
        return {
          ...prev,
          [name]: {
            ...prev[name],
            value: value.value
          }
        };
      },
      () => {
        //state will be updated and you can send API call
      }
    );
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