Creating a Basic ReactJS Home Page: A Beginner’s Guide
In this tutorial, we’ll create a simple ReactJS project with a basic home page. We will use create-react-app
to set up the project, and then we'll create a simple home page with a header, some text, and an image.
Prerequisites:
- Node.js and npm installed on your computer. If you don’t have them, you can download them from https://nodejs.org/.
- Basic understanding of HTML, CSS, and JavaScript.
Step 1: Set up the project
- Open your terminal or command prompt and run the following command to install
create-react-app
globally:
npm install -g create-react-app
- Create a new React project by running:
create-react-app simple-react-homepage
- Navigate to the project directory:
cd simple-react-homepage
- Start the development server:
npm start
Your default web browser should automatically open with the default React app at http://localhost:3000/.
Step 2: Clean up the default project
- Open the project folder in your favorite code editor.
- Delete the
src/App.css
,src/App.test.js
,src/logo.svg
, andsrc/setupTests.js
files, as we won't be using them. - Update the
src/App.js
file to remove the references to the deleted files, and replace the content with the following code:
import React from 'react';
function App() {
return (
<div>
<h1>Hello, world!</h1>
</div>
);
}
export default App;
- Update the
src/index.js
file to remove the reference to the deletedsrc/App.css
file.
Step 3: Create the home page
- Create a new folder inside the
src
folder calledcomponents
. - Inside the
components
folder, create a new file calledHomePage.js
. - Open the
HomePage.js
file and add the following code:
import React from 'react';
const HomePage = () => {
return (
<div>
<header>
<h1>My Simple React Home Page</h1>
</header>
<main>
<p>Welcome to my simple React home page! This is a basic example of a React project.</p>
<img src="https://via.placeholder.com/300" alt="Placeholder" />
</main>
</div>
);
}
export default HomePage;
- Import and use the
HomePage
component in thesrc/App.js
file:
javascriptCopy code
import React from 'react';
import HomePage from './components/HomePage';
function App() {
return (
<div>
<HomePage />
</div>
);
}export default App;
Now, if you look at your browser, you should see the simple home page with a header, some text, and a placeholder image. Congratulations! You’ve successfully created a simple React project with a home page.