r/reactjs • u/BrownCarter • Aug 10 '26
Needs Help Why can't i use suspense like this
Normal usage
<Suspense fallback={<Loading />}>
<EffectAlbums artistId={artist.id} />
</Suspense>
Why can't i do<Suspense fallback={<Loading />}>
<div></div>
<div></div>
<div></div>
</Suspense>
Why must it house just one component
0
Upvotes
5
u/MonkeyDlurker Aug 10 '26
probably because suspense doesn't work like a regular component in the dom tree. You can wrap it in Fragment like u/dschwammerl suggested
7
u/drrnclly Aug 10 '26 edited Aug 10 '26
You can use React fragments to contain those divs.
https://react.dev/reference/react/Fragment
1
u/phryneas I ❤️ hooks! 😈 Aug 10 '26
It doesn't need to, a Suspense boundary can have multiple children. Only child components can cause suspense to happen though, not normal DOM nodes.
You seem to be throwing two unrelated things together here?
2
u/lucksp Aug 10 '26
Check out the official docs with many example. Suspense can have multiple Children but they’ll all suspend until the side effects resolve.
1
u/metchera Aug 11 '26
I think It's just because you can't put the suspense on components who don't need it or components that don't fetch the same data. You can do both but, let me take an example: If you want to show a list of users for example, you can put the suspense on the whole list because they depend on the same data or it's just one data fetching.
In your example with EffectAlbums you can notice that it depends on artist.id, so it's normal. But if in the EffectAlbums you have many component that fetches independently, they also need their own suspense.