step by step create a react‐native typescript project with expo - arnabutilities/rbac-frontend GitHub Wiki

Creating a React Native project using TypeScript with Expo involves several steps. Below is a step-by-step guide to get you started.

Step 1: Install Node.js and npm

Ensure you have Node.js installed on your machine, which includes npm (Node Package Manager). You can download it from Node.js official website.

Step 2: Install Expo CLI

Expo CLI is a command-line tool to build and manage your React Native projects. Install it globally using npm or yarn.

npm install -g expo-cli

or

yarn global add expo-cli

Step 3: Create a New Expo Project

Use the Expo CLI to create a new project. Add the --template flag to specify the TypeScript template.

expo init my-react-native-ts-app --template expo-template-blank-typescript

Navigate into your project directory:

cd my-react-native-ts-app

Step 4: Start the Development Server

Start the Expo development server to ensure everything is set up correctly.

expo start

Step 5: Open Your Project in Your Editor

Open the project in your code editor (e.g., Visual Studio Code).

Step 6: Verify TypeScript Setup

You should see a tsconfig.json file in your project root. This file configures TypeScript for your project. You should also see files with .tsx extensions, indicating they are TypeScript files.

Your App.tsx file should look something like this:

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

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Open up App.tsx to start working on your app!</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Step 7: Add Type Definitions for React Native

Ensure that you have the necessary type definitions for React Native and other dependencies.

npm install @types/react @types/react-native

Step 8: Run the Project

You can now run the project on an emulator or a physical device. Use the Expo app on your device to scan the QR code displayed in the terminal or use an emulator for iOS or Android.

expo start

Step 9: Adding Additional Libraries

If you need additional libraries, you can install them using npm or yarn. For example, to add React Navigation:

npm install @react-navigation/native
npm install @react-navigation/stack
expo install react-native-screens react-native-safe-area-context

Then, configure it in your project. For example:

// App.tsx
import 'react-native-gesture-handler';
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { StyleSheet, Text, View } from 'react-native';

const Stack = createStackNavigator();

function HomeScreen() {
  return (
    <View style={styles.container}>
      <Text>Home Screen</Text>
    </View>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Step 10: Debugging and Development

Continue developing your app by adding new components, screens, and functionality. Use TypeScript to enforce type checking and improve code quality.

Summary

By following these steps, you should have a fully functioning React Native project set up with TypeScript using Expo. This setup will allow you to leverage the powerful features of TypeScript while enjoying the streamlined development experience provided by Expo.

⚠️ **GitHub.com Fallback** ⚠️