Skip cloneElement for Fragment children in TouchableHighlight#56539
Skip cloneElement for Fragment children in TouchableHighlight#56539qflen wants to merge 2 commits intofacebook:mainfrom
Conversation
|
Hi @qflen! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
|
This will break the highlight though. Would it better to generate a more actionable error here (eg TouchableHighlight doesn't support fragments)? |
|
Hm, I will check. |
b7f1c1b to
0f69734
Compare
Fixes facebook#54933. TouchableHighlight injects the underlay style onto its single child via cloneElement. React.Fragment cannot accept that style, so React emits a generic "Invalid prop `style` supplied to `React.Fragment`" warning and the highlight effect is silently broken. Surface a clear console.error in __DEV__ naming the component, the constraint, and the fix (wrap in a View). Skip the cloneElement when the child is a Fragment so React's generic warning no longer fires on top. Render the Fragment unchanged so apps relying on this since 0.79 do not crash on upgrade. Pattern matches dev error logging used elsewhere in RN such as ScrollView and TextInputState.
0f69734 to
85e74eb
Compare
|
@javache Right, the previous version silenced the warning while leaving the highlight broken. Update logs a clear The message names the component, the constraint, and the fix (wrap in a An alternative would be to switch to a hard invariant(
child.type !== React.Fragment,
'TouchableHighlight does not support React.Fragment as a child. ' +
'Wrap the children in a single host element such as <View>.',
); |
Summary
Fixes #54933.
Passing a
React.Fragmentas the child ofTouchableHighlightcurrently produces this warning:Warning: Invalid prop
stylesupplied toReact.Fragment.React.Fragment can only have
keyandchildrenprops.This happens because
TouchableHighlightunconditionally callscloneElement(child, {style})on its single child, including when that child is a Fragment.This PR skips that clone when
child.type === React.Fragmentand renders the Fragment as-is.That avoids passing an invalid
styleprop toReact.Fragmentwhile preserving the existing render path for Fragment children.Changelog:
[General] [Fixed] - Suppress
React.Fragmentstyle warning when used as a child ofTouchableHighlightTest Plan
TouchableHighlight-itest.jsthat renders<TouchableHighlight><Fragment>...</Fragment></TouchableHighlight>console.erroris not calledyarn flow focus-check packages/react-native/Libraries/Components/Touchable/TouchableHighlight.jsyarn linton the changed filesyarn prettier --list-differenton the changed files