Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Blogs

    Strategies for Cultivating a Robust Talent Pool

    B2B Leads

    Effective Networking Strategies to Generate B2B Leads

    Beginners

    Google Algorithm Update: Recent Changes to SEO

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / React / React Native / React Native Lessons / Build Real-World React Native App #8 : implement Dark mode
    React

    Build Real-World React Native App #8 : implement Dark mode

    Krissanawat KaewsanmuangBy Krissanawat KaewsanmuangDecember 29, 2020No Comments8 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Build Real-World React Native App #8 : implement Dark mode
    Build Real-World React Native App #8 : implement Dark mode
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Build Real-World React Native App #8 : implement Dark mode
    Build Real-World React Native App #8 : implement Dark mode

    In this chapter, we are going to implement a new feature in our app. The feature is called dark mode which is very prevalent in the app nowadays. The dark mode is configured into any app to make it night friendly or low light friendly. To implement the dark mode into our react native application, we are going to use the features of react-navigation and react-native-paper. This will make it easier to trigger the dark mode manually through the app itself. The aim is to make use of React Context to change the theme of the overall app. The interesting thing is that there won’t be any re-render of components while changing the theme. We are also going to make use of the higher-order function to apply theme props to different screens and components.

    We are going to implement the Settings screen in which there will be a switch to trigger on and off the dark mode. We are also going to make use of the react-native-dark-mode package. Finally, we are going to implement the configuration by which the app will automatically go into a dark mode when switching into a dark mode in the device itself.

    Initial setup

    First, we need to go to Navigator.js and import the theme components from react-navigation as shown in the code snippet below:

    import {
       NavigationContainer,
       DefaultTheme,
       DarkTheme
    } from '@react-navigation/native';

    Then, we need to import the required components for dark theme and theme properties from a react-native-paper package as shown in the code snippet below:

    import {
       Provider as PaperProvider,
       DefaultTheme as PaperDefaultTheme,
       DarkTheme as PaperDarkTheme,
    } from 'react-native-paper';
    

    Then, by using PaperProvider and theme prop we need to pass dark theme as a parameter as shown in the code snippet below:

    <PaperProvider theme={PaperDarkTheme}>
               <NavigationContainer theme={DarkTheme}

    As a result, the overall UI interface theme of the app will automatically change to dark as shown in the emulator screenshots below:

    Trigger dark mode in React Native
    Trigger dark mode in React Native

     

    Changing Theme with React Context

    Here, we are going to control theme configuration by using React Context. We need to create ThemeContext and a global function named toggleTheme and theme variable to access the theme value as shown in the code snippet below:

    import React, { createContext, useState } from 'react';
    export const ThemeContext = createContext();
    export const ThemeController = ({ children }) => {
    const [theme, setTheme] = useState(false);
       const toggleTheme = value => {
           if (value === true) {
               setTheme(true);
           } else {
               setTheme(false);
           }
       };
       return (
           <ThemeContext.Provider value={{ theme, toggleTheme }}>
               {children}
           </ThemeContext.Provider>
       );
    };

    Here, we have created a new file name ThemeCotrotroller.js with state variables and ThemeController functional component.

    Activate context

    To make context accessible in every child component, we need to go to App.js  and import ThemeController to this file as shown in the code snippet below:

    import { ThemeController } from './src/components/ThemeController';

    Then, we need to wrap the Navigator class with ThemeController as shown in the code snippet below:

    	        <ThemeController>
               <Navigators />
             </ThemeController>

    Next, we need to come back to Navigator.js and import useContext and ThemeContext as shown in the code snippet below:

    import React, { useContext } from 'react'
    import { ThemeContext } from './ThemeController'

    With the help of the useContext hook, we can access theme value easily. Then, we can use value to change theme as shown in the code snippet below:

    const { theme } = useContext(ThemeContext)
       let paper_theme = theme ? PaperDarkTheme : PaperDefaultTheme;
       let nav_theme = theme ? DarkTheme : DefaultTheme;
       const Tab = createBottomTabNavigator();
       return (
           <PaperProvider theme={paper_theme}>
               <NavigationContainer theme={nav_theme}>

    Now, to create a theme switch button, we will use Setting.js screen. First, we need to import ThemeContext in the Setting.js file as shown in the code snippet below:

    import React, { useContext, useState } from 'react';
    import { View, TouchableOpacity } from 'react-native';
    import {
       List, Switch,
    } from 'react-native-paper';
    import { ThemeContext } from '../components/ThemeController';

    Now, we can use the functions and variables as shown in the code snippet below:

    const Setting = ({ navigation }) => {
       const { toggleTheme, theme } = useContext(ThemeContext);

    Next, we use the Switch component to switch theme which depends on the theme variable used for switch status as shown in the code snippet below:

    return (
       <View style={{ flex: 1 }}>
           <List.Item
               title="Dark Mode"
               left={() => <List.Icon icon="brightness-4" />}
               right={() => <Switch value={theme} onValueChange={toggleTheme} />}
           />

    Now, we can try to switch the theme from light mode to dark mode and vice-versa using the switch button in the Settings screens as shown in the emulator screenshot below:

    Read More:  React Lesson 14: Redux Thunk Deep Dive
    Activate dark mode
    Activate dark mode

    Change Theme on HTML

    Since react-native-render-html does not depend on react-native-paper and react-navigation package, the theme for HTML elements in the screen doesn’t change automatically. Hence, we need to make a higher-order component named withTheme from react-native-paper and then inject theme value into it.

    For that, we need to import withTheme component to a screen that uses the react-native-render-html package as shown in the code snippet below:

    import {
       Card,
       Title,
       Paragraph,
       withTheme,
    } from 'react-native-paper';

    Now, we need to wrap component name with withTheme function while exporting as shown in the code snippet below:

    export default withTheme(FlatlistItem);

    Hence, we can pass theme as a parameter in this component now as shown in the code snippet below:

    const FlatlistItem = ({ item, navigation, theme }) => {

    For now, we will change the style of color using the following theme value:

                          <HTMLRender
                              html={item.excerpt.rendered}
                               tagsStyles={{
                                   p: { color: theme.colors.text },
                               }}
                           />

    Now, if we change the theme of the app again, we will get the following results on our emulator screen:

    Dark theme in HTML zone
    Dark theme in HTML zone

    No re-render problem

    Now, our final problem is that the react-native-render-html components do not re-render when the theme changes. To force the components of this package to change the style. We add key prop as shown in the code snippet below:

                           <HTMLRender
                               key={theme.dark}
                               html={item.excerpt.rendered}
                               tagsStyles={{
                                   p: { color: theme.colors.text },
                               }}
                           />

    Now, if we change the theme of the app, we will get the result as shown in the emulator screenshots below:

    Solve React Native render HTML in dark mode
    Solve React Native render HTML in dark mode

     

    Change theme on SinglePost Screen

    In SinglePost screen, we have used the components from a react-native-render-html package. So, we start here by adding a theme in FlatlistItem. First, we need to import withTheme Component as shown in the code snippet below:

    import {
       Avatar,
       Card,
       Title,
       Paragraph,
       List, withTheme
    } from 'react-native-paper';

    The withTheme module is a higher-order function provided by the react-native-paper package. When applied to a component, it injects the theme instances and objects into the component props. Then, the instances and methods provided by it can be accessed through the component’s props.

    Read More:  Using SWR for Efficient Data Fetching in Next.js Applications

    Then, we need to wrap the SinglePost component before export as shown in the code snippet below:

    export default withTheme(SinglePost)

    Then, we need to inject the theme variable into its parameter as shown in the code snippet below:

    const SinglePost = ({ route,theme }) => {

    Next, we will use the theme variable to change the HTML tag styles. The coding implementation for this is provided in the code snippet below:

                          <Card.Content>
                           <HTML
                               key={theme.dark}
                               html={post[0].content.rendered}
                               imagesMaxWidth={Dimensions.get('window').width}
                               tagsStyles={{
                                   p: { color: theme.colors.text },
                                   pre: { color: theme.colors.accent },
                                   h1: { color: theme.colors.text },
                                   h2: { color: theme.colors.text },
                                   h3: { color: theme.colors.text },
                                   li: { color: theme.colors.text },
                               }}
                           />
                       </Card.Content>

    Then, we need to add a theme to the icon as well:

    <MaterialCommunityIcons name="bookmark" size={30} color={theme.colors.text}/>

    Hence, we will get the following result on our emulator screen:

    Add dark mode to icon
    Add dark mode to icon

    As we can see, the overall theme of the SinglePost screen also changed to dark.

    Hence, we have successfully added the dark mode themes to different screens in our app and also configured the manual trigger in the Settings screen.

    Conclusion

    This chapter is the most interesting one in which we learned how to change the theme to React Native. Dark Mode is a popular feature in the latest Android updates. Many users prefer using dark mode in the device itself and especially the apps. Here, we got the detailed stepwise guidance on how to implement dark mode theme changes in the React Native app. The interesting part was to change the theme using the packages involved and manually trigger the theme change using the switch in the Settings screen. The in-built modules in react-navigation and react-native-paper packages made this implementation easier and simple.

    Now, that we know how to change the theme to dark mode. We can try implementing some different themes to the UI giving a more modern look.

    In the next chapter, we are going to learn how to handle the Offline mode in a React Native app.

    All codes are available on Github.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Krissanawat Kaewsanmuang
    • Website
    • X (Twitter)

    Developer Relation @instamobile.io

    Related Posts

    JAMstack Architecture with Next.js

    March 15, 2024

    Rendering Patterns: Static and Dynamic Rendering in Nextjs

    March 7, 2024

    Handling Mutations and Data Fetching Using React Query

    September 12, 2023
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Interview December 8, 2016

    Interview With Oleg – Soshace Team

    Hi! My name is Oleg. I’m a full-stack web developer from Russia. I’ve been developing web applications for the last 5 years. I am an excellent problem solver specializing on JavaScript development using such frameworks as React for the front-end and Express and Koa for the back-end side.

    Maximizing Startup Success: Strategic Data Utilization Techniques

    December 4, 2024

    Enhancing Recruitment: The Value of Background and Reference Checks

    December 16, 2024

    The Impact of Social Proof on Thought Leadership Marketing

    August 27, 2025

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    23. Уроки Node.js. Домены, “асинхронный try..catch”. Часть 3.

    Programming November 18, 2016

    Balancing Value-Driven Content and Promotional Messaging Strategies

    Content & Leadership August 27, 2025

    React Native AsyncStorage Example: When I Die App

    JavaScript December 31, 2019

    Уроки React . Урок 8

    Programming September 30, 2016

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    Soshace

    Happy New 2020 Year!

    Events

    Google I/O 2019: New JavaScript Features

    Entrepreneurship

    Unlocking Business Success: The Strategic Power of Storytelling

    Most Popular

    1. Уроки Node.js .Модули. Часть 1.

    Programming

    Effective Low-Cost Marketing Strategies for Startups

    Startups

    21. Node.js Lessons. Writable Response Stream (res), Pipe Method. Pt.2

    Programming
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.