Я пытаюсь использовать реактивные элементы с моим приложением 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},
},
});