From 3bc883c6c60632f6a41df3867368f16f684b3865 Mon Sep 17 00:00:00 2001 From: Andrei Shikov Date: Thu, 5 Aug 2021 12:37:19 -0700 Subject: [PATCH] Warn when negative `numberOfLines` prop set on component Summary: Updates previous variant that was crashing a surface to the non-crashing variant. Now it prints error in console and modifies value to be 0. Changelog: [General][Fixed] Clamp negative values for `numberOfLines` in component Reviewed By: yungsters Differential Revision: D30129658 fbshipit-source-id: fda47a262365573514d3e1e4bf8a26f6d30cdae0 --- Libraries/Text/Text.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Libraries/Text/Text.js b/Libraries/Text/Text.js index 52b063d5bc4b4c..f2dc30651eaff1 100644 --- a/Libraries/Text/Text.js +++ b/Libraries/Text/Text.js @@ -149,13 +149,12 @@ const Text: React.AbstractComponent< } } - const numberOfLines = restProps.numberOfLines; - if (numberOfLines != null) { - invariant( - numberOfLines >= 0, - 'Number of lines in component can not be negative, passed value: %s.', - [numberOfLines], + let numberOfLines = restProps.numberOfLines; + if (numberOfLines != null && !(numberOfLines >= 0)) { + console.error( + `'numberOfLines' in must be a non-negative number, received: ${numberOfLines}. The value will be set to 0.`, ); + numberOfLines = 0; } const hasTextAncestor = useContext(TextAncestor); @@ -165,6 +164,7 @@ const Text: React.AbstractComponent< {...restProps} {...eventHandlersForText} isHighlighted={isHighlighted} + numberOfLines={numberOfLines} selectionColor={selectionColor} style={style} ref={forwardedRef} @@ -178,6 +178,7 @@ const Text: React.AbstractComponent< allowFontScaling={allowFontScaling !== false} ellipsizeMode={ellipsizeMode ?? 'tail'} isHighlighted={isHighlighted} + numberOfLines={numberOfLines} selectionColor={selectionColor} style={style} ref={forwardedRef}