Check for negative numberOfLines in TextView

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
This commit is contained in:
Andrei Shikov
2021-08-04 05:13:57 -07:00
committed by Facebook GitHub Bot
parent 8408d723ff
commit a8a85034e1
+10
View File
@@ -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.
@@ -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 ? (