I have defined useState
hook like the following. And the function handleDownloadClick
is triggered when a user hits a button on the UI in my app (code not shown for breivity purpose).
When the control reaches else
block, I am trying to set the downloadData
which is an object to the variable selectedAsset
like this setSelectedAsset(downloadData);
. However, console.log(selectedAsset[0]);
always prints undefined
. What am I doing wrong here? Is the way I have defined the useState for an object correct?
const [selectedAsset,setSelectedAsset] = useState({});
const handleDownloadClick = (downloadData, e) => {
console.log("Inside Download button");
console.log(downloadData);
if(![1,2].includes(downloadData.assetCategoryId))
{
}
else {
console.log("June 23: Testing Inside Else statement");
console.log(downloadData);
setSelectedAsset(downloadData);
console.log("Testing selectedAsset[0] value");
console.log(selectedAsset[0]);
}
For reference:
console.log(downloadData);
looks like following in the browser's console:
Object { id: 1234, assetsTypeId: -1, fileName: "encounters_1624466728211.csv", locationTypeId: 1, path: null, fullPathName: "/mnt/nfs/Data/dev/data/encounters_1624466728211.csv", ubriteUri: null, fileVersion: null, fileEncodingId: null, ownerId: 123, … }