We use StyleSheet in almost all components. Generally there are three different approaches to changing component layout with React Native:
- Direct layout change in the component (e.g. text field)
- Create a StyleSheet section in each page
- Create a Global StyleSheet and reference in each component
Personally speaking, I do not recommend the first 2 options due to:
- maintenance and inconsistency approach in design between different components
- harder to transfer to a new project.
- with GlobalStyles, you can put on a separate windows for easy view and update as you work on different component
- apply the dark/light theme to the component directly through GlobalStyles without having to customize it for each page
Theme - Dark and Light theme with React Native with iOS, Android, Web, etc.
Warning: if you use Expo Debugging, the system will default to Light theme (at least on iOS). Remove Remote Debugging if you want the Dark Theme to show up.
In your Navigagor, apply the theme:
- const scheme = useColorScheme();
- <NavigationContainer theme={scheme === 'dark' ? DarkTheme : DefaultTheme}>
import { useTheme } from '@react-navigation/native';
In the function section, you can use the colors from the theme similar to below:
- const { colors } = useTheme();
To use the theme, you can access using the command colors.background (for background color), colors.text (for text color), etc.
Apply this concept suing the GlobalStyles stylesheet and you can use it on the stylesheet directly for all your projects - see the GlobalStyles section below.
Customized layout
Just a note, the first option could be used if you require flexibility in your component (where a conditional (if/else) needs to be used in your code or customized style needs to be applied to the component directly
- <Icon style={{...GlobalStyles.icon, color: iconColor, fontSize: iconSize }} />
GlobalStyles StyleSheet and reference Dark/Light Theme
Credit: the idea on using GlobalStyle/props/and hook to support Dark and Light Theme comes from this page: link.
For global stylesheet, create a stylesheet on a specific folder:
E.g. Add the file to the root folder: globalStyles.js
Add the code similar to be low in the file:import { StyleSheet } from 'react-native';
const globalStyles = useGlobalStyles();
<TouchableOpacity style={globalStyles.flatListRow} ....
import { useTheme } from '@react-navigation/native';
import React from 'react';
import React from 'react';
const getGlobalStyles = (props) => StyleSheet.create({
container: {
backgroundColor: props.colors.background,
},
}
function useGlobalStyles() {
const { colors } = useTheme();
container: {
backgroundColor: props.colors.background,
},
}
function useGlobalStyles() {
const { colors } = useTheme();
const styles = React.useMemo(() => getGlobalStyles({ colors }), [colors]);
return styles;
}
export default useGlobalStyles;
return styles;
}
export default useGlobalStyles;
In the component that you want to use the Global Style, import the StyleSheet:
import useGlobalStyles from '../globalStyles.js';
export default function Feed({ content, onProfileRequested, navigation }) {
Now to use the GlobalStyles in the component:
<TouchableOpacity style={globalStyles.flatListRow} ....
That is about it... easy to use and edit!
Comments
Post a Comment