Building Your First React App: A Step-by-Step Guide for Beginners
๐ Welcome to this step-by-step tutorial on building a simple React app for beginners! In this tutorial, we'll go over the basics of React and build a simple app to demonstrate its features. By the end of this tutorial, you'll have a good understanding of how React works and how to create a basic app using it.
๐ ๏ธ Prerequisites
Before we get started, make sure you have the following installed on your system:
Node.js (version 14 or above)
NPM (Node Package Manager)
๐ Getting Started
First, let's create a new React app using the Create React App tool. Open up your terminal and run the following command:
npx create-react-app my-app
This will create a new React app called "my-app" in a new directory.
Next, navigate into the "my-app" directory and start the development server by running:
cd my-app
npm start
This will start the development server and open up a new browser window with your app running.
๐งฑ Creating Components
In React, everything is a component. Let's create our first component by creating a new file in the "src" directory called "App.js". Add the following code to the file:
import React from 'react';
function App() {
return (
<div>
<h1>Hello, World!</h1>
</div>
);
}
export default App;
This creates a new component called "App" that simply returns a div with an h1 tag saying "Hello, World!".
Next, let's add our new component to the main "index.js" file. Open up "index.js" and replace the contents with the following:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
This imports our new "App" component and renders it to the DOM.
๐จ Adding Styles
Let's add some styles to our "App" component. Create a new file in the "src" directory called "App.css". Add the following styles to the file:
.App {
font-family: sans-serif;
text-align: center;
}
h1 {
font-size: 3rem;
margin-top: 5rem;
}
This will style our "App" component with a sans-serif font and center the text. It also styles the h1 tag with a large font size and a margin on the top.
Next, import the styles into our "App" component by adding the following line to the top of "App.js":
import './App.css';
๐ Testing
That's it! We've created a simple React app with a single component and some styles. Open up your browser to "localhost:3000" to see the app in action.
๐ Conclusion
In this tutorial, we covered the basics of building a simple React app. We created a new app using Create React App, created a new component, added styles to the component, and rendered the component to the DOM. React is a powerful library with many features, but this simple example should give you a good understanding of how it works.
If you have any questions or suggestions then, feel free to reach out to me on Twitter or LinkedIn. You can find me on Twitter @DivyParekh and LinkedIn at LinkedIn. I look forward to connecting with you and discussing all things!
Subscribe to my newsletter
Read articles from Divy Parekh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Divy Parekh
Divy Parekh
Hello everyone, My name is Divy Parekh and I am a passionate front-end developer who enjoys coding and bringing ideas into reality. My knowledge of HTML, CSS, JavaScript, and ReactJS is extensive. I have worked on many projects using React and JavaScript throughout my career. Here is my portfolio link:- https://effervescent-wisp-e2232d.netlify.app/