docs: clarify iterable render returns - #8606
Open
Rezakarimzadeh98 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the React class component reference docs to clarify that render may return iterables (not just arrays) of React nodes, and to warn about pitfalls when returning iterators that can be consumed across renders (notably in development).
Changes:
- Expands the “Returns” description to include iterables of React nodes.
- Adds a caveat warning about returning iterators from
renderand the need to create a fresh iterator per render.
Suppressed comments (1)
src/content/reference/react/Component.md:586
- This new iterator caveat largely repeats the Strict Mode bullet immediately below and implies extra development-only renders in general. Consider reordering so the Strict Mode explanation comes first, and tighten the iterator warning to the core issue (iterators are consumed).
- If you return an iterator from `render`, make sure each render creates a fresh iterator. React may render more than once in development, and a reused iterator may already be consumed. When possible, prefer returning an array or another reusable iterable.
- When [Strict Mode](/reference/react/StrictMode) is on, React will call `render` twice in development and then throw away one of the results. This helps you notice the accidental side effects that need to be moved out of the `render` method.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| #### Returns {/*render-returns*/} | ||
|
|
||
| `render` can return any valid React node. This includes React elements such as `<div />`, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and `false`), and arrays of React nodes. | ||
| `render` can return any valid React node. This includes React elements such as `<div />`, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and `false`), arrays of React nodes, and iterables of React nodes. |
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Clarify that
ender may return iterables of React nodes, and warn that iterators should be recreated each render.
Why
This avoids confusion around returning iterators from class components in development, where React may call
ender more than once and a reused iterator can already be consumed.
Changes