The value is not being selected in the dropdown

Here is my code. If you notice I am trying to select CountryID=1 in the dropdown but for some reason that is not getting selected -

import React, { useState, useEffect } from 'react';
import { DataManager, ODataV4Adaptor, Query } from '@syncfusion/ej2-data';
import { useMsal } from "@azure/msal-react";
import { DropDownListComponent } from '@syncfusion/ej2-react-dropdowns';
const Home = () => {
    const [loaded, setLoaded] = useState(false);
    const { instance } = useMsal();
    const [token, setToken] = useState(null);
    useEffect(() => {
        const account = instance.getActiveAccount();
        const accessTokenRequest = {
            scopes: ["api://0fcfc1ec-9df8-4f8e-b8e6-4a1ca3674696/AccessAPI", "User.Read", "Group.Read.All"],
            account: account
        };
        if (!token) {
            instance.acquireTokenSilent(accessTokenRequest).then(function (accessTokenResponse) {

                setToken(accessTokenResponse.accessToken);
            })
                .catch(function (error) {
                    if (error.name === "InteractionRequiredAuthError") {
                        instance.acquireTokenPopup(accessTokenRequest)
                            .then(function (accessTokenResponse) {

                                setToken(accessTokenResponse.accessToken);
                            }).catch(function (error) {
                                console.log(error);
                            });
                    }
                });
        }
    });

    var stateProvinceNameFromApi;
    var countryFromApi;
    if (token !== null && token !== "") {
        stateProvinceNameFromApi = new DataManager({
            url: `https://courtconnectapi.azurewebsites.net/odata/StateProvince`,
            adaptor: new ODataV4Adaptor(),
            crossDomain: true,
            headers: [{ Authorization: `Bearer ${token}` }],
        });
        countryFromApi = new DataManager({
            url: `https://courtconnectapi.azurewebsites.net/odata/Country?$select=Id,CountryName`,
            adaptor: new ODataV4Adaptor(),
            crossDomain: true,
            offline: true,
            headers: [{ Authorization: `Bearer ${token}` }],
        });
    }
    const countryId = 1;
    return (
        <div>
            <DropDownListComponent id="CountryId" value={countryId} dataSource={countryFromApi} fields={{ text: 'CountryName', value: 'Id' }} placeholder="Select Country" popHeight='300px' floatLabelType='Always'></DropDownListComponent>
        </div>
    );
}

export default Home;



Can someone help us with what I might be doing wrong?


1 Reply

KP Kokila Poovendran Syncfusion Team July 19, 2023 04:35 PM UTC

Hi Ameet Phadnis,


Upon reviewing the code you provided, we have identified that the token value is returning as null, which is causing the dataManager to be unable to set the component properly. Consequently, the action complete event returns an empty array because the data has not been fetched from the server yet. This is the reason why the initial preselected value is not being set in the component.


To resolve this issue, we kindly request you to ensure that the token is set properly before proceeding with the data fetch. Once the dataSource values have been fetched successfully, you can then proceed to set the preselected value of the component accordingly.


Screen shot : (result value returns null)



If you need further assistance or have any additional questions, please reach out to us.


Regards,

Kokila Poovendran.


Loader.
Up arrow icon