feat: Add isValidNode function to validate React nodes #32667
+92
−0
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
This PR adds a comprehensive
isValidReactNode
utility function that correctly validates all possible types of React nodes according to the React documentation. The implementation accounts for primitive values such as strings, numbers, booleans, null, and undefined - all of which are valid React nodes.Based on https://react.dev/reference/react/isValidElement#react-elements-vs-react-nodes
initially tested in Nextra https://github.com/shuding/nextra/pull/4366/files#diff-3c9997fb445c0f1b6cb3a813ca5743ad7785c6a36220c373b681f310c8817f47
This new utility function properly validates:
isValidElement
)null
andundefined
valuestrue
andfalse
)This addresses potential issues where devs create custom checkers for React nodes which might be incorrect, improving the developer experience and preventing false error conditions.
How did you test this change?
I verified this implementation with the following tests:
All tests pass. I also manually verified the function behavior in a React application by testing it with various types of content that should be renderable.