How to Import Color Variables to a File in React Native?

Oct 29, 2022 . Admin

This article will provide example of import color variables to my styles. if you have question about react native import css file as variable then I will give simple example with solution. we will help you to give example of how to import color variables to a file in react native app. We will use global colors react native.

In this example,We will create import color variable in react native.you can easy and simply use variable your stylesheet in react native.below's this example.

Step 1: Download Project

In the first step run the following command to create a project.

expo init ExampleApp
Step 2: colors.js

In this step, You will create the colors.js file and put the code.

export const Colors = {
  primary: 'red',
  secondary: '#ffffff',
}
Step 2: App.js

In this step, You will open the App.js file and put the code.

import { StyleSheet, View, Text } from "react-native";
import {Colors} from "./colors.js";
  
export default function App() {
    return (
        <View style={styles.container}>
            <Text style={ styles.textStyle }>MyWebtuts.com</Text>
        </View>
      );
    }
  
const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: Colors.primary,
      justifyContent: "center",
      alignItems: "center",
    },
    textStyle:{
      fontSize: 20,
      color:Colors.secondary,
      fontWeight: 'bold'
    }
});
Step 3: Run Project

In the last step run your project using the below command.

expo start

You can QR code scan in Expo Go Application on mobile.

Output:

It will help you...

#React Native