Fixing crash when passing null children to view with no-hide-descendents #13224
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.
Description
When passing null to the children prop of a view that uses
importantForAccessibility="no-hide-descendents"
, React Native Windows JS code wil crash due to the assumption that in this case, the children must be nonnull. React allows null and undefined on the typeReactNode
, which is the go to type for declaring the type for the children prop on a component.This is most likely a rare combination of props, though it is still valid. React.Children.map() has the following type:
map<T, C>(children: C | readonly C[], fn: (child: C, index: number) => T): C extends null | undefined ? C : Exclude<T, boolean | null | undefined>[];
The fix proposed is to check for null or undefined before using React.Children.map().
Type of Change
Why
This fixes a crash that my team in Xbox has encountered when upgrading our RNW version to 0.73 from 0.69. This is a pre-emptive fix as we caught this prior to check-in.
What
I've added defensive code to validate this codepath.
Screenshots
Add any relevant screen captures here from before or after your changes.
Testing
This is the fix we have in Xbox. Our testing is that it doesn't crash in this scenario anymore.
Changelog
yes if reasonable. This returns functionality that should be there.
"Fixed a crash that occurred when passing null to a View with
"no-hide-descendents"
set forimportantForAccessibility
.Microsoft Reviewers: Open in CodeFlow