我正在尝试将 react-native-elements 与我的 React-Native 应用程序一起使用。
我有一个带有主题详细信息的中央 js 文件,正如这里解释的那样,使用 ThemeProvider 注入 - https://react-native-elements.github.io/react-native-elements/docs/customization.html
但是,当我尝试在组件的 stylesheet.create 方法中使用传递的主题时,出现错误。我究竟做错了什么?-
import React from 'react';
import {View, StyleSheet} from 'react-native';
import {Text, withTheme} from 'react-native-elements';
const Header = props => {
const {theme} = props;
return (
<View style={styles.container}>
<Text>Home</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
width: '100%',
backgroundColor: theme.container.backgroundColor,//**** Getting error here stating that theme is not defined
shadowRadius: 1.5,
elevation: 2,
shadowColor: 'rgb(0,0,0)',
shadowOpacity: 0.4,
shadowOffset: {width: 0, height: 2.5},
},
});
export default withTheme(Header);
如果我可以提供更多详细信息,请告诉我。
更新:
感谢@Sebastian Berglönn 下面提供的建议,我能够在不导出到不同文件的情况下通过这样做来对其进行参数化 -
const Header = props => {
const {theme} = props;
return (
<View style={styles(theme).container}>
<Text>Home</Text>
</View>
);
};
const styles = theme =>
StyleSheet.create({
container: {
width: '100%',
backgroundColor: theme.container.backgroundColor,
shadowRadius: 1.5,
elevation: 2,
shadowColor: 'rgb(0,0,0)',
shadowOpacity: 0.4,
shadowOffset: {width: 0, height: 2.5},
},
});