Reactjs 使用!在反应中很重要
我在另一个Text组件中有Text组件:Reactjs 使用!在反应中很重要,reactjs,react-native,Reactjs,React Native,我在另一个Text组件中有Text组件: <Text style = {styles.firstText}> {this.props.firstText} {' '} <Text style = {styles.secondText}> second text </Text> {' '} <Text style = {styles.thirdText}>
<Text style = {styles.firstText}>
{this.props.firstText}
{' '}
<Text style = {styles.secondText}>
second text
</Text>
{' '}
<Text style = {styles.thirdText}>
{this.props.thirdText}
</Text>
</Text>
{this.props.firstText}
{' '}
第二文本
{' '}
{this.props.thirdText}
FirstText具有fontFamily=X
第二个文本需要有fontFamily=Y,但X也适用于此处
我如何在这里设置优先级
已尝试使用fontFamily:'Y'+'!重要提示“但是运气不好。你可以从他们说:
建议在应用程序中使用一致字体和大小的方法是创建包含这些字体和大小的组件MyAppText
,并在应用程序中使用此组件。您还可以使用此组件为其他类型的文本创建更具体的组件,如MyAppHeaderText
使用所需样式为文本创建包装:
class MyAppHeaderText extends Component {
render() {
return (
<MyAppText>
<Text style={{fontSize: 20}}>{this.props.children}</Text>
</MyAppText>
);
}
}
类MyAppHeaderText扩展组件{
render(){
返回(
{this.props.children}
);
}
}
并在应用程序中使用它:
<View>
<MyAppText>
Text styled with the default font for the entire application
</MyAppText>
<MyAppHeaderText>Text styled as a header</MyAppHeaderText>
</View>
使用整个应用程序的默认字体设置样式的文本
样式为标题的文本
他们还解释说发生在你身上的事情是故意的
React Native仍然有样式继承的概念,但仅限于文本子树。在本例中,第二部分将同时为粗体和红色
我很大胆
和红色
他们还解释了原因:
- (开发者)React组件的设计考虑到了强大的隔离性:您应该能够将组件放到应用程序中的任何位置,相信只要道具相同,它的外观和行为都会相同。可以从道具外部继承的文本属性将打破这种隔离
- (实现者)React Native的实现也被简化。我们不需要在每个元素上都有fontFamily字段,也不需要每次显示文本节点时都潜在地遍历树直到根。样式继承仅在本机文本组件内部编码,不会泄漏到其他组件或系统本身
<Text style={{fontWeight: 'bold'}}>
I am bold
<Text style={{color: 'red'}}>and red</Text>
</Text>