javascriptreact-nativeglobalreact-native-stylesheet

"TypeError: Cannot read property 'container' of undefined" when adding a global stylesheet to my React project


I am trying to add a global stylesheet in my project as it has same thing on different pages. So, to improve my work I want to do it. Here is my global stylesheet code

import  {StyleSheet} from 'react-native';

export const globalStyles = StyleSheet.create({
    container: {
        padding: 24,
        flex: 1,
        backgroundColor: '#fbb1'
    },
    titleText:{
        fontFamily: 'Nunito-Black',
        fontSize: 20,
        color: '#333'
    },
    paragraph:{
        marginVertical: 8,
        lineHeight: 20
    }
})

Everything goes fine until then. But whenever I try to run it, It is giving me the following error.

TypeError: Cannot read property 'container' of undefined

       5 | export default function Home() {
       6 |     return (
    >  7 |         < View style = { globalStyles.container }>
         |                                       ^
       8 |         <Text style= {globalStyles.titleText}> This is Home Screen </Text>
       9 |     </View >
      10 |     )

      at Home (screens/home.js:7:39)
      at renderWithHooks (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5552:18)
      at mountIndeterminateComponent (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7918:13)
      at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9019:16)
      at performUnitOfWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12649:12)
      at workLoopSync (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12622:22)
      at performSyncWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12333:9)
      at node_modules/react-test-renderer/cjs/react-test-renderer.development.js:1825:24
      at unstable_runWithPriority (node_modules/scheduler/cjs/scheduler.development.js:653:12)
      at runWithPriority (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:1775:10)

Here is my home screen code:

import React from 'react';
import { Text, View, Image } from 'react-native';
import{globalStyles } from '../styles/global';

export default function Home() {
    return (
        < View style = {globalStyles.container}>
        <Text style= {globalStyles.titleText}> This is Home Screen </Text>
    </View >
    
    )
}

Kindly tell me what am I doing wrong. As I am a beginner. I already tried some methods but none of them are working


Solution

  • Try with the default export

    export default StyleSheet.create({
    container: {
        padding: 24,
        flex: 1,
        backgroundColor: '#fbb1'
    },
    titleText:{
        fontFamily: 'Nunito-Black',
        fontSize: 20,
        color: '#333'
    },
    paragraph:{
        marginVertical: 8,
        lineHeight: 20
    }})   
    

    In component

    import customStyle from '../styles/global';