DEV Community

Cover image for Getting Started with React: Creating Our First Project
Glos Code
Glos Code

Posted on • Edited on

Getting Started with React: Creating Our First Project

React is a popular JavaScript library for building user interfaces. It was created by Facebook and is widely used by developers all over the world. In this tutorial, we’ll walk through the basics of React and create a simple application.

Prerequisites

Before we begin, you’ll need to have a basic understanding of HTML, CSS, and JavaScript. You should also have Node.js and npm installed on your machine. If you don’t have them already, you can download them from the official website.

Setting up the project

To create a new React project, you can use the create-react-app command-line tool. Open up a terminal and run the following command:

npx create-react-app my-app
Enter fullscreen mode Exit fullscreen mode

This will create a new directory called my-app with all the necessary files and folders for a React project.

Creating our first component

In React, everything is a component. A component is a reusable piece of code that can be used to build user interfaces. Let’s create our first component by creating a new file called HelloWorld.js in the src folder.

import React from 'react';

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

export default HelloWorld;
Enter fullscreen mode Exit fullscreen mode

In this code, we define a new function called HelloWorld that returns some JSX. JSX is a syntax extension for JavaScript that allows us to write HTML-like code inside our JavaScript files.

We then export this component using the export default syntax, so that it can be used in other parts of our application.

Using our component

Now that we’ve created our HelloWorld component, let's use it in our App component. Open up the App.js file in the src folder and replace the existing code with the following:

import React from 'react';
import HelloWorld from './HelloWorld';

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

export default App;
Enter fullscreen mode Exit fullscreen mode

In this code, we import our HelloWorld component and use it inside our App component.

Running the project

To run our project, open up a terminal and navigate to the my-app directory. Then run the following command:

npm start
Enter fullscreen mode Exit fullscreen mode

This will start a development server and open up our application in a new browser window.

Conclusion

In this tutorial, we’ve walked through the basics of React and created a simple application. We’ve learned how to create a new project, create a new component, and use that component in our application. We hope you found this tutorial helpful!

Top comments (0)