Skip to main content

Command Palette

Search for a command to run...

Getting Started With React JS: A Beginners' Guide To Setting Up A React App.

Published
•5 min read•View as Markdown
Getting Started With React JS: A Beginners' Guide To Setting Up A React App.
A

I am a Frontend Software Developer from Nigeria, a Technical Writer, an Open Source & Community Advocate, the creator and maintainer of LanguageAI, and a Speaker.

As technology continues to evolve, ReactJS remains at the forefront of modern web development, enabling developers to create scalable and efficient applications with ease. Whether you're a seasoned developer or just getting started on your coding adventure, understanding React is a valuable skill that opens doors to a vibrant ecosystem and empowers you to build exceptional user experiences, known for its flexibility and efficient rendering.

In this article, we'll take an overview look at ReactJS, explore its benefits, and guide you through setting up a React JS environment using different methods, ensuring you have the tools and knowledge needed to dive into the world of React development.

Prerequisites

Before you begin, this tutorial assumes that you already have the following:

  • NodeJS installed on your local development machine

  • Basic knowledge of HTML, CSS and JavaScript

  • A web browser for testing and previewing your React Application

  • A code editor for seamless development

  • A strong internet connection

What is ReactJS?

ReactJS, often referred to simply as React, is a declarative, efficient, and flexible JavaScript library for building user interfaces (UIs), particularly for single-page applications where UI updates are frequent. It revolves around the concept of building reusable UI components, allowing developers to efficiently update and render views as data changes. It was developed by Facebook and is now maintained by Facebook and a community of individual developers and companies.

Why Choose ReactJS?

ReactJS offers several compelling reasons to use React for building user interfaces in web applications. Here are some key advantages:

  • Component-Based Architecture: React promotes a modular and reusable component-based architecture. Components encapsulate specific functionalities and can be easily reused across different parts of an application, enhancing maintainability and scalability.

  • Virtual DOM for Performance: React's use of a virtual DOM improves performance by minimizing direct manipulation of the actual DOM. The virtual DOM allows React to update only the parts of the DOM that have changed, reducing unnecessary re-rendering and enhancing application speed.

  • Declarative Syntax: React utilizes a declarative syntax, making it easier to understand and express how the UI should look based on the application's state. This simplifies the process of updating and maintaining the UI.

  • One-Way Data Binding: React follows a unidirectional data flow, making it easier to understand how changes in data affect the application state and UI. This clear data flow contributes to a more predictable and manageable codebase.

  • JSX for Enhanced Readability: React uses JSX, a syntax extension that allows HTML/XML-like code to be written within JavaScript files. JSX enhances code readability and simplifies the process of creating and rendering UI components.

  • Rich Ecosystem: React has a vast and active community, resulting in a rich ecosystem of libraries, tools, and resources. Developers can leverage pre-built components, state management solutions (such as Redux), and various extensions to enhance development productivity.

  • React Native for Cross-Platform Development: React Native, an extension of React, enables the development of cross-platform mobile applications using React and native components. This allows developers to use React skills to build mobile apps for both iOS and Android.

  • Strong Community Support: React is maintained by Facebook and has a large and engaged community. This community support ensures continuous improvement, frequent updates, and a wealth of learning resources.

  • Compatibility with Other Technologies: React can be easily integrated with other technologies and frameworks. It is often used in combination with tools like Webpack, Babel, and various backend technologies.

  • Adoption by Major Companies: React is widely adopted by major companies, including Facebook, Instagram, Airbnb, Netflix, and others. Its popularity and usage by industry leaders indicate its reliability and suitability for large-scale applications.

Setting Up a ReactJS Environment

  1. Using Create React App (CRA)

  • Open your terminal and run the following commands:

      npx create-react-app my-react-app
      cd my-react-app
    
  • Start the development server

      npm start
    

    Then visit http://localhost:3000 in your web browser to see your React app running.

  • Explore ReactJS Components

    Dive into ReactJS by exploring and modifying the default components created in the src folder.


Manual Setup

  • Initialize a New React Project

    Create a new directory for your project and run:

      mkdir my-react-app
      cd my-react-app
      npm init -y
    
  • Install React and React DOM

      npm install react react-dom
    
  • Create a Basic HTML File

    Create an index.html file with a root div for your React app:

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My React App</title>
      </head>
      <body>
        <div id="root"></div>
        <script src="src/main.js"></script>
      </body>
      </html>
    
  • Write your first component

    Create a src directory and add a main.js file with a basic React component:

      // src/main.js
      import React from 'react';
      import ReactDOM from 'react-dom';
    
      const App = () => {
        return <h1>Hello, React!</h1>;
      };
    
      ReactDOM.render(<App />, document.getElementById('root'));
    
  • Bundle with a Module Bundler

    Use a module bundler like Webpack to bundle your React code. Install Webpack and related dependencies:

      npm install webpack webpack-cli babel-loader @babel/core @babel/preset-react --save-dev
    
  • Create a webpack.config.js file:

      // webpack.config.js
      module.exports = {
        entry: './src/main.js',
        output: {
          filename: 'bundle.js',
          path: __dirname + '/dist',
        },
        module: {
          rules: [
            {
              test: /\.js$/,
              exclude: /node_modules/,
              use: {
                loader: 'babel-loader',
                options: {
                  presets: ['@babel/preset-react'],
                },
              },
            },
          ],
        },
      };
    
  • View Your React App

    Run the following command to bundle and view your React app:

      npx webpack --config webpack.config.js
    
  • Open the index.html file in a web browser to view your React app


  1. Using Vite
  • Install Vite:

      npm create vite my-react-app --template react
      cd my-react-app
    
  • Install the necessary dependencies and start the development server:

      npm install
      npm run dev
    
  • Visit http://localhost:5173 to see your React app built with Vite.

  • Explore Vite Features

    Vite provides features like a fast development server, optimized builds, and hot module replacement for a smooth development experience.

Conclusion

ReactJS empowers developers to build dynamic user interfaces efficiently. Whether you choose Create React App for a quick start, opt for manual setup for more control, or leverage Vite for a modern development experience, understanding the setup process is essential. But my recommendation is the third option using Vite because of the edges it has over others.

Remember, continuous learning and exploration are key to mastering ReactJS and creating exceptional user experiences. As you embark on your React journey, you'll find yourself part of a vibrant community, continuously pushing the boundaries of what can be achieved in web development.

A

Great Share

10