Tutorial
Multi file components

Multi file Components

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Extensions

Extensions for VS Code

You can install the following extensions for VS Code to make your life easier.

For example in your App.tsx file if you type rnfs and press Tab it will automatically generate the following code for you.

import { View, Text } from 'react-native'
import React from 'react'
 
const App = () => {
  return (
    <View>
      <Text>App</Text>
    </View>
  )
}
 
export default App

⭐ Import Export

Import Export Explanation

  1. In our App.tsx file we will change view to SafeAreaView
import { View, Text ,SafeAreaView } from 'react-native'
import React from 'react'
 
const App = () => {
  return (
    <SafeAreaView>
      <Text>App</Text>
    </SafeAreaView>
  )
}
 
export default App
  1. In the app component I wan't to add a scroll feature so I will wrap the scrollview around the text component.
import { View, Text ,SafeAreaView, ScrollView } from 'react-native'
import React from 'react'
 
const App = () => {
  return (
    <SafeAreaView>
      <ScrollView>
      <Text>App</Text>
      </ScrollView>
    </SafeAreaView>
  )
}
 
export default App
  1. Now Create a components folder inside the main folder so that we can create our components inside it.

  2. Now make a file named FlatCards.tsx inside the components folder and add the following code or use shortcut rnfs to create a new file.

import { StyleSheet, Text, View } from 'react-native'
import React from 'react'
 
export default function FlatCards() {
  return (
    <View>
      <Text>FlatCards</Text>
    </View>
  )
}
 
const styles = StyleSheet.create({})
  1. Now How to use this component in our App.tsx file.

    • First we will import the component in our App.tsx file.
    • Then inside the return statement we will use the component using self closing tag like this <FLatCards/>
     import { View, Text ,SafeAreaView, ScrollView } from 'react-native'
     import React from 'react'
     import FlatCards from './components/FlatCards'
     
     
     const App = () => {
       return (
         <SafeAreaView>
           <ScrollView>
            <FlatCards/>
           </ScrollView>
         </SafeAreaView>
       )
     }
     
     export default App