gpt4 book ai didi

javascript - 在 React Native 中全局加载文件

转载 作者:行者123 更新时间:2023-11-29 17:45:13 25 4
gpt4 key购买 nike

我是原生 react 新手。我想知道是否有办法将 CSS、颜色全局加载​​到所有组件和屏幕,而不是在每个屏幕中导入。

我有一个可重用的 stylesheet.js 和 color.js,其中包含所有全局 css 和颜色。stylesheet.js

'use strict';

import {StyleSheet} from 'react-native';
import colors from './colors';


module.exports = StyleSheet.create({

container:{
flex:1,
},

alwaysred: {
color:colors.txt_main,
},

});

但现在要在组件中使用,我需要在每个组件中导入。所以我想知道是否有更简单的方法将样式表和颜色导入所有脚本/组件。

我尝试导入index.js,但是当我尝试访问样式属性时,它说样式未定义像这样的事情

Index.js

import { AppRegistry } from 'react-native';
import stylesheet from './app/resources/styles/stylesheet';

import App from './app/App';

AppRegistry.registerComponent('newApp', () => App);

App.js

 <Text style={stylesheet.alwaysred}>
New stylesheet imported globally
</Text>

这给出了错误,指出未定义的样式表。我关注了这个 Stackoverflow 线程 link创建全局样式表

最佳答案

使用全局:

global.stylesheet = StyleSheet.create({

container:{
flex:1,
},

alwaysred: {
color:colors.txt_main,
},

});

关于javascript - 在 React Native 中全局加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50345238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com