DEV Community

Zamora
Zamora

Posted on

Intern level: Core Concepts of React

Components

What Are Components?

Components are the building blocks of any React application. They let you split the UI into independent, reusable pieces, and think about each piece in isolation. A component in React can be thought of as a JavaScript function or class that optionally accepts inputs (known as "props") and returns a React element that describes how a section of the UI should appear.

Imagine your application as a LEGO structure, where each LEGO brick is a component. These bricks can be combined in various ways to build complex structures (applications).

Functional vs. Class Components

In React, there are two main types of components: functional components and class components.

Functional Components

Functional components are simple JavaScript functions that accept props as an argument and return a React element. They are easy to read and write and are the preferred way to create components in modern React development.

Example of a Functional Component:

import React from 'react';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode

Class Components

Class components are ES6 classes that extend React.Component and must have a render() method which returns a React element. They used to be the standard way of creating components but have become less common with the introduction of Hooks.

Example of a Class Component:

import React, { Component } from 'react';

class Greeting extends Component {
  render() {
    return <h1>Hello, {this.props.name}!</h1>;
  }
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode

Creating and Using Components

To create a component in React, you can define a function or class that returns a React element. To use this component, you can include it in the JSX of another component.

Example of Creating and Using a Functional Component:

import React from 'react';

// Define the Greeting component
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Define the App component
function App() {
  return (
    <div>
      <Greeting name="Alice" />
      <Greeting name="Bob" />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

In the above example, the Greeting component is used within the App component. The name prop is passed to Greeting and displayed inside the <h1> tag.

JSX (JavaScript XML)

Introduction to JSX

JSX stands for JavaScript XML. It is a syntax extension for JavaScript that looks similar to HTML. JSX is used with React to describe what the UI should look like. Each JSX element is just syntactic sugar for calling React.createElement(component, props, ...children).

JSX makes it easier to write and understand the structure of your UI components. Hereโ€™s an example of JSX:

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

This JSX code gets compiled to:

const element = React.createElement('h1', null, 'Hello, world!');
Enter fullscreen mode Exit fullscreen mode

Embedding Expressions in JSX

JSX allows you to embed JavaScript expressions within curly braces {}. This can include variables, function calls, or any valid JavaScript expression.

Example of Embedding Expressions in JSX:

const name = 'Alice';
const element = <h1>Hello, {name}!</h1>;

const getGreeting = (name) => `Hello, ${name}!`;
const greetingElement = <h1>{getGreeting('Bob')}</h1>;
Enter fullscreen mode Exit fullscreen mode

JSX vs. HTML

While JSX looks similar to HTML, there are a few key differences:

  1. JSX Attributes: In JSX, attributes are written in camelCase rather than lowercase. For example, class becomes className, and onclick becomes onClick.
   <div className="container"></div>
   <button onClick={handleClick}>Click Me</button>
Enter fullscreen mode Exit fullscreen mode
  1. JavaScript Expressions: In JSX, you can embed JavaScript expressions within curly braces {}, which is not possible in plain HTML.
   const isLoggedIn = true;
   <div>{isLoggedIn ? 'Welcome back!' : 'Please log in.'}</div>
Enter fullscreen mode Exit fullscreen mode
  1. Self-Closing Tags: JSX requires self-closing tags for elements without children, similar to XML.
   <img src="image.jpg" />
Enter fullscreen mode Exit fullscreen mode
  1. Fragments: In JSX, you can use fragments to group multiple elements without adding extra nodes to the DOM.
   <>
     <h1>Title</h1>
     <p>Description</p>
   </>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding components and JSX is fundamental to working with React. Components allow you to break down your UI into reusable, independent pieces, while JSX provides a syntax that closely resembles HTML, making it easier to describe your UI. As you continue to develop with React, mastering these core concepts will enable you to build efficient, maintainable, and scalable applications.

Top comments (0)