0

In React Native, you can have a separate file for styling and access that file from other pages to refer to the stylings.

'use strict';

var React = require('react-native');
const Dimensions = require('Dimensions');
var {
    StyleSheet,
    } = React;

var styles = StyleSheet.create({
    defaultBackground: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#ffffff',
    },
    menuButton: {
        fontSize: 22,
        color: '#ffffff',
        paddingHorizontal: 20,
    },
    container: {
        flex: 1
    }
});

module.exports = styles;

Here this is acquired by "StyleSheet" of React.

Say for a sample like following,

myName: 'Username',
myURL: 'www.google.com',
reactNativeUser: true,
age: 22,

Something like this to be used in a separate file as a property file and to be used across the application.

How to achieve this?

1 Answer 1

2

Export plain object

You could export a plain object. And create the stylesheet as necessary.

// style.js
export const Style = {
    defaultBackground: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#ffffff',
    },
    menuButton: {
        fontSize: 22,
        color: '#ffffff',
        paddingHorizontal: 20,
    },
    container: {
        flex: 1
    }
}

// component.js
import React, {StyleSheet} from 'react-native';
import {Style as Globalstyle} from 'style'

var styles = StyleSheet.create(Globalstyle);

Maybe I understood your question not correctly. To export those constants you could use the same approach.

//propertyfile.js
export const myName = "Username";
export const myURL = "www.google.com",
export const reactNativeUser = true;
export const age = 22;

//component.js
import {myName, myURL, reactNativeUser, age} from 'propertyfile'
Sign up to request clarification or add additional context in comments.

2 Comments

where we will be placing this file 'propertyfile.js' in project structure? it should work for ios and android right.
That’s up to you. Next to the component? Just leave out the suffix .ios or .android to use it cross-platform

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.