Skip to content

Commit

Permalink
Warn when negative numberOfLines prop set on <Text/> component
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Andrei Shikov authored and facebook-github-bot committed Aug 5, 2021
1 parent 72f7962 commit 3bc883c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,12 @@ const Text: React.AbstractComponent<
}
}

const numberOfLines = restProps.numberOfLines;
if (numberOfLines != null) {
invariant(
numberOfLines >= 0,
'Number of lines in <Text/> component can not be negative, passed value: %s.',
[numberOfLines],
let numberOfLines = restProps.numberOfLines;
if (numberOfLines != null && !(numberOfLines >= 0)) {
console.error(

This comment has been minimized.

Copy link
@gaurav5430

gaurav5430 Dec 24, 2021

Why have we moved from invariant to console.error here ?

`'numberOfLines' in <Text> must be a non-negative number, received: ${numberOfLines}. The value will be set to 0.`,
);
numberOfLines = 0;
}

const hasTextAncestor = useContext(TextAncestor);
Expand All @@ -165,6 +164,7 @@ const Text: React.AbstractComponent<
{...restProps}
{...eventHandlersForText}
isHighlighted={isHighlighted}
numberOfLines={numberOfLines}
selectionColor={selectionColor}
style={style}
ref={forwardedRef}
Expand All @@ -178,6 +178,7 @@ const Text: React.AbstractComponent<
allowFontScaling={allowFontScaling !== false}
ellipsizeMode={ellipsizeMode ?? 'tail'}
isHighlighted={isHighlighted}
numberOfLines={numberOfLines}
selectionColor={selectionColor}
style={style}
ref={forwardedRef}
Expand Down

0 comments on commit 3bc883c

Please sign in to comment.