DEV Community

Cover image for React 101: How to Set Up Your Kitchen and Bake Your First Component with Create React App
ruthcyg
ruthcyg

Posted on

React 101: How to Set Up Your Kitchen and Bake Your First Component with Create React App

Cooking up a React App with Create React App is akin to preparing a delightful feast in your kitchen. React, a beloved JavaScript library, allows you to whip up reusable components for displaying data and handling user interactions. But before diving into the culinary adventure of creating React apps, you’ll need to equip your kitchen with the right tools and ingredients. Enter Create React App, your trusty kitchen assistant.
**
What’s in the Create React App Kitchen?**
Create React App is like a seasoned baker that sets up a modern React development environment with zero configuration. It takes care of essentials like:
**
Web Server**: Serves your app in development mode, automatically reloading when changes are made. This server acts as your oven, baking your app and presenting it to you in the browser

Bundler: Optimizes and chunks your code for faster production loading. Think of this as a dough mixer, combining and optimizing your code for production. _It splits your code into smaller chunks for faster loading, just like kneading dough for a better rise.
_
Compiler: Transforms JSX code into plain JavaScript, supporting the latest language features.

Linter: Checks your code for errors and enforces best practices._ Imagine this as a quality inspector, checking your code for errors and enforcing best practices. It helps you maintain clean, consistent code, just like a clean kitchen produces better results._

Test Runner: Allows you to write and run unit tests for your components. Think of this as a food critic, evaluating your components and ensuring they meet expectations. It helps you write and run unit tests, assuring that your app functions as intended.

Service Worker: Enables offline functionality and faster loading on subsequent visits. This is like a pantry stocker, keeping your app’s resources cached for offline use and faster loading on repeat visits. It improves the user experience, just like having a well-stocked pantry makes cooking more convenient.
In essence, Create React App acts as your chef, meticulously preparing your kitchen with all the necessary equipment and ingredients, so you can focus on the art of cooking.

Cooking Steps with Create React App
To start your React cooking journey, ensure you have Node.js and npm installed. These are like the fundamental ingredients in your pantry.

Create a new React app by running one of these commands in your terminal:

npx create-react-app my-app (for npm 5.2+)
npm init react-app my-app (for npm 6+)
yarn create react-app my-app (for Yarn 0.25+)
This creates a project structure resembling a well-organized kitchen, with folders for pantry (public), stove (src), fridge (node_modules), and a recipe (package.json).

**
Preparing and Serving Your Dish**
To start the development server and serve your app, run either npm start or yarn start. This opens your browser to http://localhost:3000, showcasing the default React app provided by Create React App. Modify code in the src folder, save, and witness automatic browser reloads.

Stop the development server with Ctrl + C in your terminal.

Crafting Your First React Component
In the world of React, a component is your culinary masterpiece, a piece of UI with its own logic and appearance. To create one, write a JavaScript function returning JSX. Remember, function names must start with a capital letter.

For instance, let’s create a simple Greeting component:
function Greeting() {
return (
<div>
<h1>Hello, world!</h1>
<p>Welcome to React.</p>
</div>
);
}

Render this component in index.js, the entry point of your app:

`import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Greeting from './Greeting';

ReactDOM.render(


,
document.getElementById('root')
);`

Your culinary creation, the Greeting component, is now served in the browser. Behold the greeting message as your first React component takes center stage. Bravo, you’ve successfully cooked up and served your inaugural React dish!

Happy cooking!

Happy Coding !
https://medium.com/@ewho.ruth2014




Enter fullscreen mode Exit fullscreen mode

Top comments (0)