Article image Components

6. Components

Page 6 | Listen in audio

In React, components are the building blocks of the user interface. They allow developers to break down complex UIs into smaller, reusable pieces that can be managed independently. Understanding components is crucial for anyone looking to master React. In this section, we'll dive deep into components, exploring their types, lifecycle, and best practices for creating efficient and maintainable code.

Understanding Components

Components in React can be thought of as JavaScript functions or classes that accept inputs, called "props," and return React elements that describe how a section of the UI should appear. These components can be composed together to build complex UIs. There are two main types of components in React: functional components and class components.

Functional Components

Functional components are the simplest form of React components. They are JavaScript functions that return a React element. They are called "functional" because they are typically written as pure functions, meaning they don't rely on or modify external state.


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

The above example demonstrates a functional component named Greeting that takes a single prop, name, and returns a simple greeting message. Functional components have become more popular with the introduction of Hooks in React, which allow them to manage state and side effects.

Class Components

Class components are more feature-rich than functional components and are defined using ES6 classes. They allow the use of lifecycle methods and can hold their own state.


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

The Welcome component is a class component that extends React.Component. It uses the render method to return a React element. Class components were the standard way to manage state and lifecycle methods before the introduction of Hooks.

Props and State

Props and state are two important concepts in React that help manage data in components.

Props

Props, short for properties, are read-only inputs passed to components. They are used to pass data from parent components to child components, enabling communication between different parts of the application. Props are immutable, meaning a component cannot change its own props.


function Display(props) {
  return <p>The value is: {props.value}</p>;
}

In the above example, the Display component receives a prop called value and displays it. Props are passed to components as attributes in JSX.

State

State is a built-in object that allows components to manage their own local data. Unlike props, state is mutable and can be changed by the component itself. This makes state ideal for managing dynamic data that changes over time, such as user input or data fetched from an API.


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

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

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.increment}>Increment</button>
      </div>
    );
  }
}

The Counter component maintains its own state, count, which is initially set to 0. The increment method updates the state using this.setState(), triggering a re-render of the component with the new state value.

Lifecycle Methods

Lifecycle methods are special methods in class components that allow developers to hook into different stages of a component's lifecycle, such as when it is mounted, updated, or unmounted. Some common lifecycle methods include:

  • componentDidMount(): Invoked immediately after a component is mounted. It's a good place to initiate network requests or subscriptions.
  • componentDidUpdate(prevProps, prevState): Called immediately after updating occurs. It's a good place to handle updates based on changes to props or state.
  • componentWillUnmount(): Invoked immediately before a component is unmounted and destroyed. It's used to clean up resources, such as timers or subscriptions.

With the introduction of Hooks, functional components can now also manage lifecycle events using the useEffect Hook.


import React, { useState, useEffect } from 'react';

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setSeconds(prevSeconds => prevSeconds + 1);
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return <p>Seconds: {seconds}</p>;
}

In the Timer component, the useEffect Hook is used to set up a timer that increments the seconds state every second. The cleanup function, returned by useEffect, clears the interval when the component is unmounted.

Best Practices for Components

When working with components, following best practices can help create efficient and maintainable code:

Keep Components Small and Focused

Each component should focus on a single responsibility. This makes components easier to understand, test, and reuse. If a component becomes too complex, consider breaking it down into smaller sub-components.

Use PropTypes for Type Checking

PropTypes is a React feature that allows developers to specify the types of props a component should receive. This helps catch bugs early by ensuring that components receive the correct data types.


import PropTypes from 'prop-types';

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

Greeting.propTypes = {
  name: PropTypes.string.isRequired
};

Use Destructuring for Props

Destructuring props can make code cleaner and more readable by allowing direct access to prop values without repeatedly writing props..


function UserProfile({ name, age }) {
  return <p>{name} is {age} years old.</p>;
}

Optimize Re-Renders

Frequent re-renders can affect performance. Use techniques like React.memo for functional components or shouldComponentUpdate for class components to prevent unnecessary re-renders.


const MemoizedComponent = React.memo(function MyComponent(props) {
  return <div>{props.value}</div>;
});

Conclusion

Components are the heart of React applications, enabling developers to build complex UIs from simple, reusable pieces. By understanding the types of components, managing state and props effectively, leveraging lifecycle methods, and following best practices, you can create efficient and maintainable React applications. As you continue to work with React, you'll find that mastering components is a fundamental step in becoming a proficient React developer.

Now answer the exercise about the content:

What are the two main types of components in React?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Components: Component Composition

Next page of the Free Ebook:

7Components: Component Composition

8 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text