Skip to main content

React Native - Global StyleSheet and Dark/Light Theme design approach

We use StyleSheet in almost all components. Generally there are three different approaches to changing component layout with React Native:

  1. Direct layout change in the component (e.g. text field)
  2. Create a StyleSheet section in each page
  3. Create a Global StyleSheet and reference in each component
Personally speaking, I do not recommend the first 2 options due to:
  1. maintenance and inconsistency approach in design between different components
  2. harder to transfer to a new project.
  3. with GlobalStyles, you can put on a separate windows for easy view and update as you work on different component
  4. 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}>

In the import section, import the useTheme:

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 }} />
<- Customized is on the red-text

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';
import { useTheme } from '@react-navigation/native';
import React from 'react';

const getGlobalStyles = (props) => StyleSheet.create({
    container: {
        backgroundColor: props.colors.background,
    },
}

function useGlobalStyles() {
    const { colors } = useTheme();
    const styles = React.useMemo(() => getGlobalStyles({ colors }), [colors]);
    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:

const globalStyles = useGlobalStyles();
<TouchableOpacity style={globalStyles.flatListRow} ....

That is about it... easy to use and edit!





 

Comments

Popular posts from this blog

Checked! Task Pro - Blackberry Instruction

  Checked Instruction I just started working on this page, please send me comments and suggestions that could improve this site. Please note that if you use the Android app, the instruction is similar to the BlackBerry app below. I will create a separate instruction page for Android app soon. Contents 1  Initial Set-up after Installation 2  SMART task system that combine the best of GTD (Getting Things Done) and MYN  3  Smart Add - natural language date parser 4  ***NEW*** Shortcuts 5  Back-up and Restore - How to Transfer data from one device to another 6  BES - Work Space - Add a public app from the BlackBerry World storefront 7  Settings options that will definitely increase your productivity 8  Automatically add email as task 9  Tips on improving performance 10  Add a category 11  Delete a category 12  Keep categories permanently 13  Add sub-task 14  Automatic Synchronize from remember 15  Sort opti...

Checked! Task & Calendar Pro - Support Guide

Checked! Task & Calendar Pro - Support Guide Welcome to the support page for Checked! Task & Calendar Pro ! This guide will help you get the most out of your task and calendar management experience. Quick Links Getting Started View Modes Task Management Calendar Integration Categories & Organization Notes & Attachments Common Questions (FAQ) Tips & Tricks Preferences Guide Troubleshooting Getting Started Welcome to Checked! Checked! Task & Calendar Pro is a comprehensive task and calendar management app designed to help you stay organized and productive. The app seamlessly integrates your tasks with your calendar events, giving you a complete view of your day. Key Features at a Glance Multiple View Modes : Switch between List, Day Timeline, and Monthly views Calendar Integration : Sync with your existing Apple Calendar events Smart Task Creation : Create tasks with natural language date parsing Categories & Organization : Or...

Gentle Unscented Skin Care

Gentle Unscented Skin Care Guide ⚠️ IMPORTANT DISCLAIMER This is for my own tracking and usage. As of March 2025: This list is provided for informational purposes only. Product formulations can change, and items may be discontinued or recalled. ALWAYS verify product ingredients and consult with a dermatologist before use, especially if you have sensitive skin, allergies, or medical conditions. Use at your own risk. Table of Contents Body Soaps Hand Cleansers Facial Cleansers Body Moisturizers Hand Moisturizers Facial Moisturizers Deodorant/Antiperspirant Shaving Creams Shampoo Sunscreens Hair Sprays/Gels Laundry Detergents Fabric Softeners Hand Dermatitis Care Body Soaps Dove Sensitive Skin Bar Soap Neutrogena Bar Soap Aveeno Moisturizing Bar CeraVe Hydrating Cleanser or Cleansing Bar Lipikar Syndet Cetaphil Restoraderm Nourishing Body Wash Mother Dirt Cleanser...