React is one of the most popular web frameworks out there. It has been growing steadily in popularity for years, passing Angular for the first time in the 2019 Stack Overflow developer survey.
This post will show you how to create your own React website in just a few minutes. If you're interested in learning more after completing this tutorial, checkout the Beginning React course I just created on Next Tech to further improve your React skills.
For now, let's dive right into building a website with React!
Prerequisites
To complete these steps you'll need to have the Node Package Manager (npm) installed. If you don't have it installed yet, head on over to https://www.npmjs.com/get-npm to download and install npm.This will also installnpx
which we'll use to runcreate-react-app
.
Create React App
Create React App is an excellent way to quickly get a React website up and running. Create React App was created by Facebook (the same company that created React!). In their docs, they describe it as:Create React App is an officially supported way to create single-page React applications. It offers a modern build setup with no configuration.Knowing that Create React App is supported by the creators of React is a huge plus. Let's use it to get started with our website!
Run the following command to create your site:
Run the following command to create your site:
npx create-react-app hello-react
Viewing the React website
Next, run the following commands to start the React development server:cd hello-react
npm start
Updating the site
Now, let's make a change to update the site. Open thehello-react/src/App.js
file, then replace the following line:Edit src/App.js
and save to reload.
My first React website!
Creating a React Component
Next, we'll create a new React component. First, create a folder in thesrc
folder named components
. Then create a file called HomepageImage.js
in the src/components
folder. This file will hold our new homepage image component.We'll create this component by adding the following code to the
HomepageImage.js
file:import React from 'react';
function HomepageImage() {
const url = 'https://cdn.filestackcontent.com/XYrHCaFGRSaq0EPKY1S6';
return (
<img src={url} style={{width: 650}} alt='Image of Golden Gate Bridge' />
);
}
export default HomepageImage;
App.js
, replace<img src={logo} className="App-logo" alt="logo" />
<HomepageImage />
App.js
by adding the following code to the top of the file:import HomepageImage from './components/HomepageImage'
import logo from './logo.svg';
App.js
file should look like this:import React from 'react';
import './App.css';
import HomepageImage from './components/HomepageImage'
function App() {
return (
<div className="App">
<header className="App-header">
<HomepageImage />
<p>
My first React website!
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
Congratulations on creating your first website using React 🎉!
Next steps
This tutorial was a quick introduction to creating web pages with React. If you want to gain a better understanding of React so you can build awesome sites using it, checkout the course I just released that teaches React!Have you built a site with React? Feel free to share your URL or a link to your project on GitHub in the comments below to show it off!
Special thanks to Maarten van den Heuvel for taking the photo of the Golden Gate Bridge used in this post!