react-native - 如何在样式表创建中使用 useTheme 钩子(Hook)?

标签 react-native themes react-native-paper

我想在组件内的样式表创建函数中使用来自我的 native 纸张主题的颜色。这是我的代码

import { StyleSheet, Text, View } from 'react-native';
import { useTheme } from 'react-native-paper';

const Home = () => {
  const { colors } = useTheme();

  return (
    <View style={[styles.container, { backgroundColor: colors.background }]}>
      <Text>Home Page</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});


export default Home;
我想在样式表中使用 backgroundColor: "#fff"来引用来自 useTheme 钩子(Hook)的 color.background。这可能吗?

最佳答案

我更喜欢这样使用它

const makeStyles = (colors: any) => StyleSheet.create({
   container: {
       backgroundColor: colors.red,
   }
})
然后,在渲染()
  const Home = () => {
  const { colors } = useTheme();
  const styles = makeStyles(colors)
  return (
    <View style={[styles.container, { backgroundColor: colors.background }]}>
      <Text>Home Page</Text>
    </View>
  );
}

关于react-native - 如何在样式表创建中使用 useTheme 钩子(Hook)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66864061/

相关文章:

reactjs - UI 库的 React Native 性能。我应该使用哪个 : Native base vs React-Native paper?

react-native - 将 React-Native-Paper BottomNavigation 与 StackNavigator 相结合

javascript - 为什么我的列表项调用了它不应该调用的函数?

android - react native Flatlist renderItem 不更新

ios - 在 React-native 中,如何更改 NavigatorIOS 的样式

javascript - 如何使用 native react 在平面列表中显示对象数组中的数组列表?

extjs - 使用 sencha cmd 生成 extjs 4.2 应用程序,默认主题名称不起作用

android - 用于创建 android UI 主题的工具

javascript - 为什么在 IOS 上选择 List.Accordion 时会以白色突出显示?

css - wordpress子主题两次加载子样式,没有父样式