Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!