Javascript 对文本元素反应本机样式
我已经这样定义了JSXJavascript 对文本元素反应本机样式,javascript,html,css,react-native,Javascript,Html,Css,React Native,我已经这样定义了JSX <View style={[color: 'red']}> <Text>Text1</Text> <Text>Text2</Text> <Text>Text3</Text> </View> 视图组件没有颜色属性,并且不应用子元素的文本颜色设置 我不想在单个元素上应用相同的样式。 如何在父元素上定义样式并应用于所有子元素(如HTML中的normal)?据我所知,没有简单的方法从
<View style={[color: 'red']}>
<Text>Text1</Text>
<Text>Text2</Text>
<Text>Text3</Text>
</View>
视图组件没有颜色属性,并且不应用子元素的文本颜色设置
我不想在单个元素上应用相同的样式。
如何在父元素上定义样式并应用于所有子元素(如HTML中的normal)?据我所知,没有简单的方法从super Componnet继承样式 也许您可以自定义如下组件:
class YourComponent extends Component {
render() {
let children = [];
React.Children.forEach(this.props.children, (item, index) => {
// textStyle is the style that you want children to inherit
children.push(React.cloneElement(item, this.props.textStyle));
});
return (
<View>
{ children }
</View>
);
}
}
据我所知,没有简单的方法可以从super Componnet继承风格 也许您可以自定义如下组件:
class YourComponent extends Component {
render() {
let children = [];
React.Children.forEach(this.props.children, (item, index) => {
// textStyle is the style that you want children to inherit
children.push(React.cloneElement(item, this.props.textStyle));
});
return (
<View>
{ children }
</View>
);
}
}
样式正在文本组件之间传播,因此您可以执行以下操作:
<Text style={{color: 'red'}}>
<Text>Text1</Text>
<Text>Text2</Text>
<Text>Text3</Text>
</Text>
样式正在文本组件之间传播,因此您可以执行以下操作:
<Text style={{color: 'red'}}>
<Text>Text1</Text>
<Text>Text2</Text>
<Text>Text3</Text>
</Text>
除了将样式单独应用于每个元素之外,您还可以使用类似lodash uz.map的东西,通过编程方式渲染每个子元素,并声明样式一次 你这样做是因为你想在每个文本标签上使用不同的颜色吗 为了减少代码,您也可以对其进行解构
const { aStyle, anotherStyle, andAnotherStyle } = styles;
而不是styles.aStyle。styles.anotherStyle….除了对每个元素单独应用样式之外,您可以使用类似lodash.map的东西,以编程方式渲染每个子元素,并声明一次样式 你这样做是因为你想在每个文本标签上使用不同的颜色吗 为了减少代码,您也可以对其进行解构
const { aStyle, anotherStyle, andAnotherStyle } = styles;
而不是styles.aStyle。风格,另一种风格