DEV Community

Cover image for Introduction to React: Building Your First Component
John Schibelli
John Schibelli

Posted on • Originally published at schibelli.dev

Introduction to React: Building Your First Component

In this inaugural article of the "Mastering React Fundamentals: A Developer's Guide" series, we'll embark on a journey to understand the core concepts of React, a powerful JavaScript library for building user interfaces. Our focus will be on setting up the development environment and creating your first React component.

Introduction to React

React is a JavaScript library developed by Facebook for building user interfaces, particularly single-page applications where efficient data updating and rendering are crucial. It allows developers to create reusable UI components, leading to more manageable and scalable code.

Setting Up the Development Environment

Before diving into React, ensure your development environment is ready:

  • Node.js and npm: React relies on Node.js and npm (Node Package Manager) for managing packages. Download and install them from the official Node.js website.

  • Code Editor: Choose a code editor like Visual Studio Code, which offers excellent support for JavaScript and React.

Creating a New React Application

React provides a tool called Create React App to set up a new project with a sensible default configuration. Here's how to use it:

  1. Install Create React App:

    Open your terminal and run:

    npx create-react-app my-first-react-app
    

    This command creates a new directory named my-first-react-app with all the necessary files and dependencies.

  2. Navigate to the Project Directory:

    cd my-first-react-app
    
  3. Start the Development Server:

    npm start
    

    Your application will run at http://localhost:3000/, and any changes you make will automatically reload the page.

Understanding JSX

JSX (JavaScript XML) is a syntax extension for JavaScript that resembles HTML. It's used in React to describe what the UI should look like. JSX makes the code more readable and easier to write. For example:

const element = <h1>Hello, world!</h1>;
Enter fullscreen mode Exit fullscreen mode

This JSX code is transformed into standard JavaScript by tools like Babel.

Creating Your First Component

Components are the building blocks of a React application. A component can be a function or a class that returns a React element. Here's how to create a simple functional component:

Define the Component:

  1. Create a new file named Greeting.js in the src directory with the following content:

    import React from 'react';
    
    function Greeting() {
      return <h1>Welcome to React!</h1>;
    }
    
    export default Greeting;
    
  2. Use the Component:

    In src/App.js, import and use the Greeting component:

    import React from 'react';
    import Greeting from './Greeting';
    
    function App() {
      return (
        <div>
          <Greeting />
        </div>
      );
    }
    
    export default App;
    

    Now, when you run your application, it will display "Welcome to React!".

Conclusion

In this article, we've set up the development environment and created our first React component. Understanding these basics is crucial as we delve deeper into React's capabilities in the upcoming articles. Stay tuned as we explore components, props, state, and more in the next installments of this series.

Top comments (0)