Skip to main content

Command Palette

Search for a command to run...

Building Your First React Application

Updated
5 min read
Building Your First React Application
G

Hello! I'm a dedicated software developer with a passion for coding and a belief in technology's impact on our world. My programming journey started a few years ago and has reshaped my career and mindset. I love tackling complex problems and creating efficient code. My skills cover various languages and technologies like JavaScript, Angular, ReactJS, NodeJs, and Go Lang. I stay updated on industry trends and enjoy learning new tools. Outside of coding, I cherish small routines that enhance my workday, like sipping tea, which fuels my creativity and concentration. Whether debugging or brainstorming, it helps me focus. When I'm not coding, I engage with fellow developers. I value teamwork and enjoy mentoring newcomers and sharing my knowledge to help them grow. Additionally, I explore the blend of technology and creativity through projects that incorporate art and data visualization. This keeps my perspective fresh and my passion alive. I'm always seeking new challenges, from open-source contributions to hackathons and exploring AI. Software development is more than a job for me—it's a passion that drives continuous learning and innovation.

React is one of the most popular libraries for building user interfaces, particularly single-page applications. Its component-based architecture makes it easy to develop and maintain complex UIs. If you're just beginning your journey into the world of web development or looking to expand your skill set, creating your first React application is a great place to start. In this blog post, we'll walk through the process step by step, implementing a simple "Hello World" app, setting up a proper folder structure, utilizing routing with React Router, and exploring UI frameworks like Material UI and Bootstrap.

1. Creating a Simple "Hello World" App

Installing Node.js and Create React App

Before you start building your React application, two critical tools you’ll need are Node.js and the Create React App command line tool. Node.js is a JavaScript runtime that allows you to run JavaScript on the server, while Create React App is a tool that sets up a new React project with sensible defaults.

  1. Install Node.js: Go to the official Node.js website and download the installer for your operating system. Follow the installation instructions.

  2. Install Create React App: Open your terminal or command prompt and run the following command:

     npx create-react-app my-first-app
    

    This command creates a new folder called my-first-app containing all the boilerplate code and dependencies needed to start a React application.

Building the Hello World Component

After creating your React app, navigate to the newly created directory:

cd my-first-app

Open the project in your favorite code editor. You can find the src folder, which contains an App.js file. This is where we’ll create our first component.

Modify the App.js file to look like this:

import React from 'react';

function App() {
  return (
    <div>
      <h1>Hello World!</h1>
    </div>
  );
}

export default App;

Running Your Application

To view your application in the browser, run the following command:

npm start

The development server will start, and you can visit http://localhost:3000 in your web browser. You should see "Hello World!" displayed on the page.

2. Setting Up Folder Structure

A well-organized folder structure is crucial for maintaining and scaling your application over time. Here's a suggested folder structure for your React app:

my-first-app/
├── public/
│   ├── index.html
│   └── ...
├── src/
│   ├── components/   // Reusable components
│   ├── pages/        // Different pages for the application
│   ├── styles/       // CSS or styled components
│   ├── App.js        // Main application component
│   ├── index.js      // Entry point of the application
│   └── ...
├── package.json
└── ...

Creating the Folder Structure

Create the new folders using the following commands in your terminal:

mkdir src/components
mkdir src/pages
mkdir src/styles

Moving Components

As you build your application, you'll find it helpful to move different parts into the respective folders. For example, if you create a new component called Greeting.js, you would place it in the components folder like this:

// src/components/Greeting.js
import React from 'react';

const Greeting = () => {
  return <h1>Hello from the Greeting Component!</h1>;
};

export default Greeting;

Update App.js to use the Greeting component:

import React from 'react';
import Greeting from './components/Greeting';

function App() {
  return (
    <div>
      <Greeting />
    </div>
  );
}

export default App;

3. Implementing Routing with React Router

Routing is essential for complex applications with multiple views. React Router makes it easy to handle navigation between different components.

Installing React Router

To add routing capabilities, you'll first need to install React Router:

npm install react-router-dom

Setting Up Routing

Let's create a new page component and set up the routes. Create a new file in the pages directory:

// src/pages/Home.js
import React from 'react';

const Home = () => {
  return <h1>Welcome to the Home Page!</h1>;
};

export default Home;

Next, create an About.js page in the same way:

// src/pages/About.js
import React from 'react';

const About = () => {
  return <h1>About Us</h1>;
};

export default About;

Now, update your App.js file for routing:

import React from 'react';
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';

function App() {
  return (
    <Router>
      <nav>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/about">About</Link>
          </li>
        </ul>
      </nav>
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/about" component={About} />
      </Switch>
    </Router>
  );
}

export default App;

Now, you have a basic navigable React application! You can visit the Home and About pages by clicking on the links.

4. Introduction to UI Frameworks (Material UI, Bootstrap)

When building user interfaces, using a UI framework can save you time and help you create visually appealing applications without deep knowledge of CSS.

Using Material UI

Material UI is a popular React UI framework that implements Google's Material Design. You can install Material UI by running:

npm install @mui/material @emotion/react @emotion/styled

Using Material UI components, update your navigation in App.js:

import { AppBar, Toolbar, Button } from '@mui/material';

// Inside your App component, replace nav with Material UI AppBar
<AppBar position="static">
  <Toolbar>
    <Button color="inherit" component={Link} to="/">Home</Button>
    <Button color="inherit" component={Link} to="/about">About</Button>
  </Toolbar>
</AppBar>

Run your application again, and you will notice a fresh UI without much effort.

Using Bootstrap

Another option is Bootstrap, a popular CSS framework. To integrate Bootstrap, add the following link to your public/index.html within the <head> tag:

<link
  href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
  rel="stylesheet"
/>

Now, you can enhance your app further using Bootstrap classes. Replace the navigation with:

<nav className="navbar navbar-expand-lg navbar-light bg-light">
  <div className="collapse navbar-collapse">
    <ul className="navbar-nav mr-auto">
      <li className="nav-item">
        <Link to="/" className="nav-link">Home</Link>
      </li>
      <li className="nav-item">
        <Link to="/about" className="nav-link">About</Link>
      </li>
    </ul>
  </div>
</nav>

Conclusion

In this post, you learned how to set up your first React application, create a "Hello World" component, structure your project properly, implement routing with React Router, and explore UI frameworks like Material UI and Bootstrap. Each of these elements is crucial for developing more advanced applications.

The world of React is vast and continually evolving. As you progress with your learning, don't hesitate to explore React's rich ecosystem of libraries and tools. Remember, building a solid foundation will pave the way for creating powerful, dynamic web applications in the future. Happy coding!

More from this blog

H

Hacker's Haven

28 posts

I'm a Senior Software Developer with expertise in programming languages and tech. Follow me for detailed articles and tutorials on software development tools and frameworks.