Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the useTheme hook inside my Stylesheet create?

I want to use the colors from my react native paper theme inside the stylesheet create function inside my component. Here's my code

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;

I want to have the backgroundColor: "#fff" in the stylesheet to reference the colors.background coming from the useTheme hook. Is this possible?

like image 456
laneherby Avatar asked Mar 30 '21 02:03

laneherby


1 Answers

I prefer to use it like this

const makeStyles = (colors: any) => StyleSheet.create({
   container: {
       backgroundColor: colors.red,
   }
})

then, in render()

  const Home = () => {
  const { colors } = useTheme();
  const styles = makeStyles(colors)
  return (
    <View style={[styles.container, { backgroundColor: colors.background }]}>
      <Text>Home Page</Text>
    </View>
  );
}
like image 126
maphongba008 Avatar answered Mar 03 '23 21:03

maphongba008