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.

Answer

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
      }
    );


Source: stackoverflow