I’m trying to load data for a client component. I can’t use await/async
as specified by React’s RFC about use()
hook. Thus I need to use use()
hook.
However, it goes into infinite loop.
Here’s my code:
import { use } from 'react'
const Component = () => {
const response = use(fetch('some_url'))
const data = use(response.json())
return <div>{data}</div>
}
Based on my intuition, I tried to use a callback inside the use()
hook, as we do for useEffect()
:
const response = use(() => fetch('use_url'), [])
But it complained that an invalid parameter is passed into the use()
hook.
I can’t find anyting online. What should I do? What have I done wrong?
2
Answers
You need to "stabilize" the return of
fetch
. You could memoize itI guess you just wanna memorized
use
result.