Categories: softare development

How to Use External Style Sheet in React Native

Style sheets are very useful in software development as they define both the style and the general look and feel of your software application.

Sometimes these style sheets could internal or external. One advantage of using external stylesheet in React Native is that it avoids unnecessary re-styling: one block of style can serve multiple pages.

This is usually done in HTML and CSS, and now let’s see how we can replicate that behaviour in our React Native application.

1. First Create your Style.js file (Or whatever name you prefer to use)

This is the first step in creating our external style sheet in React Native. We must create the file where our styles will be defined. Sometimes these files could be inside a folder like assets or maybe styles, but that arrangement is subjective and not compulsory, Nevertheless, it makes your project look tidy.

2. Next, Define the Styles

We will then proceed to add the styles in our created Style.js file as follows:

import { StyleSheet } from 'react-native';

const styles = StyleSheet.create({

box: {
    width: '80%',
    height: 150,
    backgroundColor: 'red',
    alignSelf: 'center',
    borderRadius: 9
  }
   
});

export { styles }

Notice here that we are importing ‘React’ or anything of such as it is not necessary.

Also notice that we are exporting style constant, which is what makes the style to be globally accessible by all screens in our application.

3. Add External Style Sheet to Our React Native App

So we’ll add external style sheet to our screen

Let’s call our screen Home.js

import React, { Component } from "react";
import { View } from 'react-native';
import { styles } from "./Style";

class Home extends Component {
    render(){
        return(
            <View>
                <View style={styles.box}>

                </View>
            </View>
        )
    }
}

export default Home;

4. Finally, We Run the App

External style sheets in React Native

This is how we create and use external style sheet in React Native.

See also:

How to create onboarding screen in React Native

View Comments

Recent Posts

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

24 hours ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

5 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

6 days ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

7 days ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago

Why Netflix Dumped React For its Frontend

Netflix, a pioneer in the streaming industry, has always been at the forefront of adopting…

1 week ago