Skip to content

Commit 3bc883c

Browse files
Andrei Shikovfacebook-github-bot
Andrei Shikov
authored andcommitted
Warn when negative numberOfLines prop set on <Text/> 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 <Text> component Reviewed By: yungsters Differential Revision: D30129658 fbshipit-source-id: fda47a262365573514d3e1e4bf8a26f6d30cdae0
1 parent 72f7962 commit 3bc883c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Libraries/Text/Text.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,12 @@ const Text: React.AbstractComponent<
149149
}
150150
}
151151

152-
const numberOfLines = restProps.numberOfLines;
153-
if (numberOfLines != null) {
154-
invariant(
155-
numberOfLines >= 0,
156-
'Number of lines in <Text/> component can not be negative, passed value: %s.',
157-
[numberOfLines],
152+
let numberOfLines = restProps.numberOfLines;
153+
if (numberOfLines != null && !(numberOfLines >= 0)) {
154+
console.error(
155+
`'numberOfLines' in <Text> must be a non-negative number, received: ${numberOfLines}. The value will be set to 0.`,
158156
);
157+
numberOfLines = 0;
159158
}
160159

161160
const hasTextAncestor = useContext(TextAncestor);
@@ -165,6 +164,7 @@ const Text: React.AbstractComponent<
165164
{...restProps}
166165
{...eventHandlersForText}
167166
isHighlighted={isHighlighted}
167+
numberOfLines={numberOfLines}
168168
selectionColor={selectionColor}
169169
style={style}
170170
ref={forwardedRef}
@@ -178,6 +178,7 @@ const Text: React.AbstractComponent<
178178
allowFontScaling={allowFontScaling !== false}
179179
ellipsizeMode={ellipsizeMode ?? 'tail'}
180180
isHighlighted={isHighlighted}
181+
numberOfLines={numberOfLines}
181182
selectionColor={selectionColor}
182183
style={style}
183184
ref={forwardedRef}

0 commit comments

Comments
 (0)