I am not getting useState variable(selectedItem) updated value after updating it in useEffect. I have 2 useEffect first one dependency array is empty and i am changing dropdown value based on role.
In Second UseEffect Dependency array I have DropDown Value, there when i access DropDown Value, I am always getting Default value for my Dropdown. I am not able to understand why this is happening and how i can fix it. How to get selectedItem value based on role in other useEffect.
import { IDropdownOption } from '@fluentui/react';
import React from 'react';
import { useState } from 'react';
const [selectedItem, setSelecteditem] = useState<IDropdownOption>({
key: "Default",
text: "Default",
});
function MyFirstComponent(props: any)
{
React.useEffect(() => {
async function setDropDownValue() {
if (props.userRole == "Admin") {
setSelecteditem({
key: "Admin",
text: "Admin",
});
} else if (props.userRole == "Support") {
setSelecteditem({
key: "Support",
text: "Support",
});
} else {
setSelecteditem({
key: "Default",
text: "Default",
});
}
}
setDropDownValue();
}, []);
React.useEffect(() => {
async function setDetailsBasedOnDropDownValue() {
if (selectedItem?.key == "Admin")
{
//Business Logic
}
else if (selectedItem?.key == "Support")
{
//Business Logic
}
else
{
//Business Logic
}
}
setDetailsBasedOnDropDownValue();
}, [selectedItem.key]);
}