albert
albert

Reputation: 339

How to resolve useMemo output

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

Answers (1)

Filip Kaštovský
Filip Kaštovský

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

Related Questions