Reputation: 339
I want to get an output of useMemo result:
const output=useMemo(()=>{
let output=(async function () {
await getPlantInfoById(itemId)
.then(item => {
return item;
})
})();
return output;
},[itemId])
But when I track the output variable via inspect element:
console.log(output)
It prints pending.
Why? How to get a preferred result?
Upvotes: 2
Views: 806
Reputation: 2006
useMemo
cannot be used to resolve asychronous values. But this behavior can be replicated using the useState
and useEffect
hooks:
const [plantData, setPlantData] = useState();
useEffect(() => {
getPlantInfoById(itemId).then(setPlantData);
}, [itemId]);
Upvotes: 3