I'm new in React framework and I'm facing following issue: When I'm trying to call backend api after assigning some values, the api is called before the assigned values are updated in the state.
Let me show you the code to get a better understanding:
// define state in RFC
const [item, setItem] = useState("");
// get value when user click submit button
const itemInputOnClickHandler = ()=> {
let comingItemValue = document.getElementById('item-input').value;
setItem(comingItemValue);
submitHandler();
}
// call api
const submitHandler = async() => {
await axios.post("API_URL");
}
// button in html:
<Button label="submit" onClick={itemInputOnClickHandler} className="m-3 btn" />
I tried many aproaches but still get the same result: The api is called before the assigned values are updated in the state.
How can I solve this so the api is called after the state has been updated?