I think this question has kind of been answered before, but I've tried looking through all the similar-titled questions and I'm still a bit confused.
My code requests some data from the what3words API, and then the data is given a variable within an object using setState. However, it is out of sync, so the console.log(modifiedData)
shows slug
as an empty string. But when you click the submit button again, slug
has been updated.
const api = require("@what3words/api");
const [modifiedData, setModifiedData] = useState({
title: '',
description: '',
category: '',
condition: '',
capacity: 0,
slug: '',
lng: 0,
lat: 0,
});
const handleSubmit = async e => {
e.preventDefault();
api.convertTo3wa({lat: modifiedData.lat, lng: modifiedData.lng}, 'en')
.then(function(getSlug) {
setModifiedData((prev) => ({
...prev,
slug: getSlug.words,
}));
console.log(modifiedData);
});