Skip to content

Commit

Permalink
Check for negative numberOfLines in TextView
Browse files Browse the repository at this point in the history
Summary:
Negative `numberOfLines` prop is not supported by Android and causes a crash during layout measurement. This change adds a check in JS to catch the error earlier.

Changelog: [Internal]

Reviewed By: GijsWeterings

Differential Revision: D30047103

fbshipit-source-id: 4248a0f573c3b6facd25c7ae6ce007a357a1469b
  • Loading branch information
Andrei Shikov authored and facebook-github-bot committed Aug 4, 2021
1 parent 8408d72 commit a8a8503
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {NativeText, NativeVirtualText} from './TextNativeComponent';
import {type TextProps} from './TextProps';
import * as React from 'react';
import {useContext, useMemo, useState} from 'react';
import invariant from 'invariant';

/**
* Text is the fundamental component for displaying text.
Expand Down Expand Up @@ -148,6 +149,15 @@ 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],
);
}

const hasTextAncestor = useContext(TextAncestor);

return hasTextAncestor ? (
Expand Down

0 comments on commit a8a8503

Please sign in to comment.