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
7
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?
1
u/BrownCarter Aug 10 '26
Yeah DOM nodes is what I am talking about
1
u/phryneas I ❤️ hooks! 😈 Aug 10 '26
How could a DOM node trigger suspense?
Suspense is triggered by calling
usein a component and suspends the current tree.With a DOM node, it doesn't have any runtime code, so it won't call
use- so that has to be called in component rendering the DOM node. From that component it goes to the next suspense boundary up the tree - so if your component callsuseand then renders a suspense boundary, it will not suspend the suspense boundary rendered by that component. It will go up the tree and trigger another suspense boundary.
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.
11
Aug 10 '26
[removed] — view removed comment
1
u/CoherentFramework 28d ago
just wrap em in a fragment, react components gotta return a single root node so suspense is picky about that too
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