悬念反应不用于获取数据
我正在试图使用悬念在未解决诺言的同时渲染后备。我在做什么错?
它应该渲染后备,而提取承诺不会返回数据。但是,当我尝试从浏览器中获取数据时,就不会出现后备。
export const MainFlow = () => {
const URL_BASE = 'https://api.thedogapi.com/v1/images/search'
const [data, setData] = useState<any[]>([])
const fetchData = async () => {
const response = await httpService.request({
url: `${URL_BASE}?limit=10`,
method: 'get',
headers: { 'x-api-key': 'some-key' }
})
setData(response.body)
}
useEffect(() => {
fetchData()
}, [])
const DogImage: FC<DogImageProps> = ({ url }) => {
return (
<img src={url} alt="I'am a doggo." className="mfone__dog-images" />
)
}
return (
<div id="mfone__dog-container">
<Suspense fallback={<div>Loading...</div>}>
<div id="mfone__images-box">
{data
? data.map((element, index) => {
return (
<DogImage key={index} url={element.url} />
)
})
: null}
</div>
</Suspense>
<div id="mfone__links-box">
<Link to="/" className="mfone__dog-links">
Back to Home page
</Link>
<button
onClick={() => fetchData()}
className="mfone__dog-links"
>
More Doggos
</button>
</div>
</div>
)
}
我试图将悬疑标签放入具有ID“ mfone__images-box ”的DIV中,但仍然无法正常工作:
return (
<div id="mfone__dog-container">
<div id="mfone__images-box">
<Suspense fallback={<div>Carregando...</div>}>
{data
? data.map((element, index) => {
return (
<DogImage key={index} url={element.url} />
)
})
: null}
</Suspense>
</div>
<div id="mfone__links-box">
<Link to="/" className="mfone__dog-links">
Back to Home page
</Link>
<button
onClick={() => fetchData()}
className="mfone__dog-links"
>
More Doggos
</button>
</div>
</div>
)
I'am trying to use Suspense to render a fallback while the Promise is not solved. What am I doing wrong?
It's supposed to render the fallback while the fetch promise don't return the data. But when i try to fetch the data from the browser the fallback does not appear.
export const MainFlow = () => {
const URL_BASE = 'https://api.thedogapi.com/v1/images/search'
const [data, setData] = useState<any[]>([])
const fetchData = async () => {
const response = await httpService.request({
url: `${URL_BASE}?limit=10`,
method: 'get',
headers: { 'x-api-key': 'some-key' }
})
setData(response.body)
}
useEffect(() => {
fetchData()
}, [])
const DogImage: FC<DogImageProps> = ({ url }) => {
return (
<img src={url} alt="I'am a doggo." className="mfone__dog-images" />
)
}
return (
<div id="mfone__dog-container">
<Suspense fallback={<div>Loading...</div>}>
<div id="mfone__images-box">
{data
? data.map((element, index) => {
return (
<DogImage key={index} url={element.url} />
)
})
: null}
</div>
</Suspense>
<div id="mfone__links-box">
<Link to="/" className="mfone__dog-links">
Back to Home page
</Link>
<button
onClick={() => fetchData()}
className="mfone__dog-links"
>
More Doggos
</button>
</div>
</div>
)
}
I have tried to put the Suspense tag inside the div that has the id "mfone__images-box", but still not working:
return (
<div id="mfone__dog-container">
<div id="mfone__images-box">
<Suspense fallback={<div>Carregando...</div>}>
{data
? data.map((element, index) => {
return (
<DogImage key={index} url={element.url} />
)
})
: null}
</Suspense>
</div>
<div id="mfone__links-box">
<Link to="/" className="mfone__dog-links">
Back to Home page
</Link>
<button
onClick={() => fetchData()}
className="mfone__dog-links"
>
More Doggos
</button>
</div>
</div>
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
截至目前,
悬疑
仅在实现组件的懒惰加载时起作用,它不支持您的用例,这就是数据获取。在这种情况下,您可以创建一个加载状态,该加载状态设置为true
,一旦数据获取功能开始执行,然后将其设置为false
完成后,这样您可以根据该状态显示加载/获取UI。更新:
悬念
现在可以在实现React Server组件时用于数据获取目的,这些组件已从实验转变为全面支持。 next.js v13 已开始在其仍然实验的app
中开始实现React Server组件目录功能。As of now,
Suspense
only works when implementing lazy loading of a component, it doesn't support your use case which is data fetching. In this case, you could create a loading state which is set totrue
as soon as the data fetching function starts executing and then, set tofalse
once it completes, that way you can display the loading/fetching UI based on that state.Update:
Suspense
can now be used for data fetching purposes when implementing React Server Components, which have moved from experimental into full support. Next.js v13 has started implementing React Server Components in its still experimentalapp
directory feature.