反应本机全局样式 [英] React Native global styles
问题描述
我是 React 的新手,我了解基于组件的内联样式的好处.但我想知道是否有一种体面的方式来拥有某种全球风格.例如,我想在整个应用程序中使用相同的文本和按钮颜色.
I'm new with React and I understand the benefits of the component based, inline styles. But I'm wondering if there is a decent way to have some sort of global style. For instance, I'd like to use the same Text and Button coloring throughout my app.
与其在每个组件中重复(如果需要,随后必须在 x 处更改它),我最初的想法是在它自己的文件中创建一个基本"样式表类并将其导入我的组件中.
Rather than repeat in every component(and subsequently have to change it in x places if need be), my initial thought is to create a 'base' StyleSheet class in it own file and import it in my components.
有更好或更反应"的方式吗?
Is there a better or more 'React' way?
推荐答案
您可以创建一个可重用的样式表.示例:
You may create a reusable stylesheet. Example:
style.js
'use strict';
import { StyleSheet } from 'react-native';
module.exports = StyleSheet.create({
alwaysred: {
backgroundColor: 'red',
height: 100,
width: 100,
},
});
在您的组件中:
const s = require('./style');
...然后:
<View style={s.alwaysred} ></View>
这篇关于反应本机全局样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!