DEV Community

Cover image for Introduction to React: Setting up Your First React Project
Devarshi Shimpi
Devarshi Shimpi

Posted on • Updated on • Originally published at blog.stonecss.com

 

Introduction to React: Setting up Your First React Project

πŸ‘‰ Original Blog Post : Link

We will also go over some basic concepts of React, including components, JSX, and state. By the end of this tutorial, you will have a basic React project up and running and be well on your way to building amazing things with React.

What is React?

React is a JavaScript library for building user interfaces. It was developed by Facebook and is often used for building single-page applications (SPAs) and mobile applications. One of the key features of React is the ability to break a user interface down into reusable components. This makes it easy to build and maintain complex applications by allowing developers to focus on smaller pieces of the puzzle rather than the entire application at once.

Setting up Your First React Project Using create-react-app

The create-react-app utility is a quick and easy way to set up a new React project. It includes a development server, a build script, and a basic folder structure, all without requiring any configuration on your part. To use create-react-app, you will need to have Node.js and npm installed on your machine.

To set up a new React project using create-react-app, open a terminal and run the following command:

npx create-react-app my-project

Replace "my-project" with the desired name for your project. This will create a new folder with the given name and set up a basic React project inside it.

Once the project has been set up, navigate to the project folder and start the development server by running the following command:

cd my-project
npm start
Enter fullscreen mode Exit fullscreen mode

This will start the development server and open a new browser window with your React project running. Any changes you make to the code will automatically be reflected in the browser.

one

Basic Concepts of React

Now that you have your first React project set up, let's go over some basic concepts of React.

Components

In React, a component is a piece of UI that can be reused throughout an application. Components can be either class-based or functional, and can accept props (short for "properties") as input.

Here is an example of a simple class-based component:

import React, { Component } from 'react';

class MyComponent extends Component {
  render() {
    return <h1>Hello, world!</h1>;
  }
}

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

This component simply renders a "Hello, world!" message to the screen.

JSX

JSX is a syntax extension for JavaScript that allows you to write HTML-like code in your React components. It makes it easy to write declarative code that describes the UI of your application.

Here is an example of a component using JSX:

import React from 'react';

function MyComponent() {
  return (
    <div>
      <h1>Hello, world!</h1>
      <p>This is a paragraph.</p>
    </div>
  );
}

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

In this example, the component returns a div element with an h1 and a p element inside it.

It's important to note that JSX is not actually HTML. It gets transpiled by Babel into regular JavaScript code that React can understand. This means that you can use JavaScript expressions inside JSX by enclosing them in curly braces. For example:

import React from 'react';

function MyComponent() {
  const name = 'John';
  return <h1>Hello, {name}!</h1>;
}

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

This code will render an h1 element with the text "Hello, John!"

State

In React, state refers to the data or variables that determine a component's behavior. In a class-based component, state is a property of the component that can be accessed using the this keyword. You can update the state of a component by using the setState method.

Here is an example of a class-based component with state:

import React, { Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  handleClick = () => {
    this.setState({
      count: this.state.count + 1
    });
  }

  render() {
    return (
      <div>
        <h1>{this.state.count}</h1>
        <button onClick={this.handleClick}>Click me</button>
      </div>
    );
  }
}

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode

In this example, the component has a state variable called "count" that starts at 0. The component also has a button that, when clicked, increments the count by 1 using the setState method. The current count is displayed in an h1 element.

Conclusion

That's it for this tutorial on setting up your first React project! I hope you found it helpful and are excited to start building amazing things with React.

Thank You for reading till here. Meanwhile you can check out my other blog posts and visit my Github and my socials here .

I am currently working on DVS Tech Labs

This article was created with the help of AI

Top comments (2)

Collapse
 
neezza profile image
Flinois Dyklan • Edited

create react app is not the most recommended build .. often ViteJS is more adequate for fast building . dont you think ?

Collapse
 
devarshishimpi profile image
Devarshi Shimpi

True, NextJs is a great option on top of React as well for Static Site Generation!! The series is for complete beginners so I thought to keep it simple and beginners-friendly! But i have kept your suggestion in mind. I may make a blogpost regarding that in the series as well.
Cheers!!

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.