How to Build a React Project With Create React App in 10 Steps
How to Build a React Project With Create React App in 10 Steps
Forum Donate
Throughout this guide, I've also included a lot of helpful tips I've
learned through building apps with Create React App to make your
workflow even easier.
To create a new React project, we can use the tool npx , provided
you have an npm version of at least 5.2.
Note: You can check what npm version you have by running
in your terminal npm -v
Using npx also ensures that we are using latest version of Create
React App to create our project:
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 2/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Create React App also gives us some templates to use for specific
types of React projects.
To create a React app that uses TypeScript, we can use the Create
React App TypeScript template:
my-react-app
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
└── src
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 3/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
What are each of these files and folders for? Forum Donate
Forum Donate
npm start
Learn to code — free 3,000-hour curriculum
When we run our project, a new browser tab will automatically open
on our computer's default browser to view our app.
It's coming from the App.js file within the src folder. If we head over
to that file, we can start making changes to our app code.
// src/App.js
function App() {
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 5/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
return (
<div className="App">
Forum Donate
<header className="App-header">
Learn to code — free 3,000-hour curriculum
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
// src/App.js
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1>React Posts Sharer</h1>
</header>
</div>
);
}
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 6/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
When you save by using Command/Ctrl + S, you will see our page Donate
Forum
immediately update to look like this:
Learn to code — free 3,000-hour curriculum
Note: The only time you may need to refresh the browser
when working with Create React App is when you have an
error.
It includes all of the packages you need to run tests using the React
Testing Library ( @testing-library/react ).
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 7/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Note: Tests will be run in all files that end in .test.js when you
run the command npm run test
// src/App.test.js
PASS src/App.test.js
Note: When run the test command, you do not need to start
and stop it manually. If you have a failing test, you can jump
into your app code, fix your error, and once you hit save, all
tests will automatically re-run.
// src/index.js
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 9/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-sc
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png"
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</nos
<div id="root"></div>
</body>
</html>
The entire React app is attached to this HTML page using the div
with the id of root you see above.
We can see that it includes meta tags for a title, description, and
favicon image (the little icon in the browser tab).
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 10/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
In our case, we can change the title to our app name and the
Forum Donate
description to suit the app we're making:
Learn to code — free 3,000-hour curriculum
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-sc
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="App for sharing peoples' posts from around the web
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png"
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React Posts Sharer</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</nos
<div id="root"></div>
</body>
</html>
What's interesting is that we are importing a file from our src folder,
as if it was a variable being exported from that file.
// src/App.js
import "./App.css";
import logo from "./logo.svg";
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 11/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
function App() {
return (
Forum Donate
<div className="App">
Learn to code — free 3,000-hour curriculum
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1>React Posts Sharer</h1>
</header>
</div>
);
}
We can import image files and other static assets directly into our
React components. This feature comes from Create React App's
webpack configuration.
// src/App.js
import "./App.css";
function App() {
return (
<div className="App">
<header className="App-header">
<img src="/logo.svg" className="App-logo" alt="logo" />
<h1>React Posts Sharer</h1>
</header>
</div>
);
}
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 12/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Any file that's placed in the public folder can be used in .js or .css files
Forum Donate
with the syntax: /filename.extension .
Learn to code — free 3,000-hour curriculum
// src/App.js
function App() {
return (
<div className="App">
<header className="App-header">
<Logo style={{ height: 200 }} />
<h1>React Posts Sharer</h1>
</header>
</div>
);
}
In other words, we can use our imported svg just like we would a regular
component.
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 13/24
Step 7. How to Install
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Forum Donate
For our post sharing app that we're making, let's grab some post
data to display in our app from the JSON Placeholder API.
{
"name": "my-react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "^0.21.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.2",
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 14/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
"web-vitals": "^1.0.1"
}
Forum Donate
}
Learn to code — free 3,000-hour curriculum
After that, you can simply restart your development server and
rename any React file that ends with .js to .tsx and you have a
working React and TypeScript project.
We'll call this component Posts, so let's create a folder within src to
hold all of our components and put a file within it: Posts.js.
To fetch our posts, we will request them from JSON Placeholder, put
them in a state variable called posts, and then map over them to
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 15/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
// src/components/Posts.js
function Posts() {
const [posts, setPosts] = React.useState([]);
React.useEffect(() => {
axios
.get("http://jsonplaceholder.typicode.com/posts")
.then((response) => setPosts(response.data));
}, []);
return (
<ul className="posts">
{posts.map((post) => (
<li className="post" key={post.id}>
<h4>{post.title}</h4>
<p>{post.body}</p>
</li>
))}
</ul>
);
}
We are fetching and returning our post data from the Posts
component, but to see it in our app, we need to import it into the
App component.
Let's head back to App.js and import it by going into the components
folder and getting the Posts component from Posts.js.
After that, we can place our Posts component under our header :
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 16/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
// src/App.js
Forum Donate
function App() {
return (
<div className="App">
<header className="App-header">
<img src="/logo.svg" className="App-logo" alt="logo" />
<h1>React Posts Sharer</h1>
</header>
<Posts />
</div>
);
}
And we can see all of our fetched posts on our home page below our
header:
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 17/24
Step 9: How to Style our App with
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Forum
CSS
Donate
Create React App comes with CSS support out of the box. If you
head to App.js, you can see at the top that we are importing an
App.css file from src.
Note: You can import .css files into any component you like,
however these styles will be applied globally to our app. They
are not scoped to the component into which the .css file is
imported.
/* src/App.css */
.App {
text-align: center;
margin: 0 auto;
max-width: 1000px;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
.App-header {
margin: 0 auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
}
li {
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 18/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
list-style-type: none;
}
Forum Donate
.post h4 {
font-size: 2rem;
}
In it, we can add some additional properties for the body element to
make our background dark and our text white:
/* src/index.css */
body {
background-color: #282c34;
color: white;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Ro
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 19/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Forum Donate
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 20/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Forum Donate
Compiled successfully.
Learn to code — free 3,000-hour curriculum
File sizes after gzip:
46.62 KB build/static/js/2.1500c654.chunk.js
1.59 KB build/static/js/3.8022f77f.chunk.js
1.17 KB build/static/js/runtime-main.86c7b7c2.js
649 B build/static/js/main.ef6580eb.chunk.js
430 B build/static/css/main.5ae9c609.chunk.css
It helps to give us an idea of the size of our app files because the size
of our .js files in particular can make a large impact on our app's
performance.
If we did not have this cache-busting hash for each of our files, we
likely couldn't see any changes we made to our app.
Finally, we can run our built React project locally with the help of the
npm package serve .
This is helpful to detect any errors we might have with the final
version of our project before pushing live to the web.
npx serve
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 21/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Ready to level up your React skills? Click the link below to get
started!
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 22/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Forum Donate
Click to join the Learn React App
Learn to code — free 3,000-hour curriculum
Reed Barger
React developer who loves to make incredible apps.
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
thousands of freeCodeCamp study groups around the world.
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 23/24
17/05/2023, 22:28 How to Build a React Project with Create React App in 10 Steps
Trending Guides
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/how-to-build-a-react-project-with-create-react-app-in-10-steps/ 24/24