React Native is a framework that allows you to build mobile apps using JavaScript and React. Here are some steps on how you can use JavaScript in a React Native app:
1. Environment Setup
- Install Node.js. You can download it from the official website.
- Install npm (Node Package Manager). It is included when you install Node.js.
- Install React Native CLI globally using npm. Run the following command in your terminal: `npm install -g react-native-cli`
- Install Android Studio or Xcode for mobile OS emulation.
1. Create a new project
- Initialize a new project using React Native CLI. Run the following command in your terminal: `react-native init YourProjectName`
1. Writing JavaScript in React Native
- Go to your project’s directory using the terminal.
- Open the directory in your code editor.
- Inside your project’s directory, find App.js file, this is the file where you can start writing your JavaScript code.
1. JavaScript Syntax in React Native
- If you’re familiar with JavaScript, you will see that syntax is very similar but it uses JSX style. JSX is a syntax extension for JavaScript. It is used with React to describe what the user interface should look like.
- Import necessary components from ‘react’ and ‘react-native’ at the top of your JavaScript file like this:
\`\`\`
import React, {Component} from ‘react’;
import {Platform, StyleSheet, Text, View} from ‘react-native’;
\`\`\`
- Write your own component by extending the Component class from React. In the render function, you can return what you want to display on the screen using JSX.
\`\`\`
export default class App extends Component{
render() {
return (
1. Running the app
- To run the app, you need to use emulator or a real device.
- For iOS, you can use: `react-native run-ios`
- For Android, you can use: `react-native run-android`
Remember, each time you save and update your JavaScript code, you will see the changes in your emulator in Real Time thanks to the React Native’s Hot Reload feature.