React basics: Creating our first React app

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I'm a firm believer in learning by doing. For this series, we'll be going through React step by step.
In this part, we'll create our first app and have a basic look at creating components that we can use.
To create our first app, we need to run the following command.
The my-app part is the name of your app.
npx create-react-app my-app
Note are reading it right. It's
npxnotnpmfor this command
We can also start as a typescript app by running the following command:
npx create-react-app my-app --template typescript
However, for this application, we'll use the plain version.
Now run the following command in the my-app folder to fire up the application:
npm start
This command will open up a browser, and you should see the basic React boilerplate.

React is a component-based system, meaning everything you see is made up of components.
Open your project in your favorite editor, and let's see how we can start creating our components.
Let's open the src/App.js file and start from a clean slate.
function App() {
return <div className='App'>My react app</div>;
}
export default App;
This will render the text "My react app". Nothing fancy yet, but that's ok.
Let's add our first-ever custom component, which will be a bookshelf.
const Bookshelf = () => {
return <div>Bookshelf</div>;
};
I've chosen to place this code above our function App declaration to keep things simple for now.
To use this component we need to modify our app to render the following:
function App() {
return (
<div className='App'>
<Bookshelf></Bookshelf>
</div>
);
}
If we reload our page now, we should see the word "Bookshelf".
This might not sound fancy yet, but you might be starting to grasp the concept of how to use components here.
Let's also create a book component.
const Book = () => {
return <div>Book</div>;
};
However, instead of the fixed name "Book", we want to give this book a title when we use it.
Let's add a title property for that!
const Book = ({ title }) => {
return <div>{title}</div>;
};
We can now import this book in our app like so:
function App() {
return (
<div className='App'>
<Bookshelf></Bookshelf>
<Book title='Laravel collections' />
</div>
);
}
And you should see a book with the title "Laravel collections" show up.
You might see where we are going with this, as we want the book to be inside a bookshelf. Let's change that and add some more books.
<Bookshelf>
<Book title='Laravel collections' />
<Book title='Ruby for beginners' />
<Book title='CSS is awesome' />
</Bookshelf>
But, if we now refresh, we don't get to see anything! That's because our bookshelf doesn't know how to render child components.
For that to work we need to define our bookshelf to accept children and render them like so:
const Bookshelf = ({ children }) => {
return <div>{children}</div>;
};
Now we should be able to see all our books!
If we add some more stuff to our page, we quickly end up with a massive file that contains all kinds of components.
And that's where React is super strong. We can simply move these components to their respective files.
Let's see how that would work by creating a components folder in your src.
Then add Bookshelf.js as a file and add the Bookshelf code like so:
export default function Bookshelf({ children }) {
return <div>{children}</div>;
}
Then we can use this component in our App.js file like so:
import Bookshelf from './components/Bookshelf';
Go ahead and try the same for the Book component.
By extracting these components, we get much cleaner and maintainable code.
I hope you enjoyed building your first React app. You can find the complete code example on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter