4

Im new to React-Native and I cant seem to understand the need for export default App Have a look at the code below

 import React from 'react';
 import { StyleSheet, Text, View } from 'react-native';

 class App extends React.Component {
 render() {
  return (
     <View style={styles.container}>
       <Text>Hello World! This is my first program</Text>
     </View>
      );
  }
 }
 
 const styles = StyleSheet.create({
   container: {
     flex: 1,
     backgroundColor: '#fff',
     alignItems: 'center',
     justifyContent: 'center',
   }

export default App;

So if I comment out the last line and run it using expo I encounter errors... else it works fine can someone explain the need for the last line ( I am just running this file,am not linking any other files)

I tried searching on the net and all I encountered was loading modules from the source file to current working file.... I didn't find any for the same file...

EDIT1:@pnizzle cleared it up.

So "export" is the key to allow access to other components (even if you don't link this file to other files),it has to be exposed in order for the RN code to run.

DeeTee
  • 66
  • 7

1 Answers1

1

"It is used to export single class, function, or primitive from a script file"

Have a look at this detailed answer.

Without the export you are in simple terms not exposing App and therefore nothing can use/access it.

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • Yes but I am not going to "export them" to be used in other files...I am just confining this code to this particular file. – DeeTee Feb 17 '21 at 05:39
  • 1
    @DeeTee your app needs an entry point. Do you have another entry point to be used if you comment out the export? – pnizzle Feb 17 '21 at 05:43
  • So in simple terms "export" is like a gate? If commented out the gate is sealed shut and if not it is open allowing entry to the Metro bundler? – DeeTee Feb 17 '21 at 05:45
  • 1
    @DeeTee yes `export` is like opening a gate with password - the password is the name of the exported thing. `export default` is opening a gate without password - person importing doesn't actually have to know the name if they use the default import (without `{}`). – jave.web Apr 10 '22 at 19:04