codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

4 / 61 topics
1Introduction to React.js2Setting Up Your Development Environment3JSX: Basics and Syntax4Components Introduction5Functional Components6Class Components7Props: Introduction and Usage8State: Introduction and Usage9Event Handling in React
Tutorials/React.js/Components Introduction
⚛️React.js

Components Introduction

Updated 2026-04-20
3 min read

Components Introduction

Overview

React is a declarative, efficient, and flexible JavaScript library for building user interfaces. At its core, React revolves around the concept of components, which are reusable pieces of code that encapsulate UI functionality and can be composed together to build complex applications.

In this tutorial, we will dive deep into the fundamentals of React components, including their types, creation, usage, and best practices. By the end of this section, you should have a solid understanding of how to create, manage, and utilize components in your React applications.

What is a Component?

A component in React is a reusable piece of UI that can be composed together to build complex user interfaces. Components encapsulate their own logic, state, and presentation, making them highly modular and maintainable. There are two main types of components in React: Functional Components and Class Components.

Functional Components

Functional components are the simplest form of components in React. They are JavaScript functions that return JSX (JavaScript XML), which describes what should be rendered on the screen.

// Example of a functional component
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

Class Components

Class components were the primary way to create components in React before the introduction of hooks. They are ES6 classes that extend React.Component and include a render() method that returns JSX.

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

Creating Components

Functional Components

Functional components are created by defining a JavaScript function that returns JSX. They can accept props (properties) as arguments and use them to customize the output.

// Example of a functional component with props
function Greeting(props) {
  return <h2>Welcome, {props.name}!</h2>;
}

// Usage
<Greeting name="Alice" />

Class Components

Class components are created by defining an ES6 class that extends React.Component. The render() method is used to define the JSX output.

// Example of a class component with props
class Greeting extends React.Component {
  render() {
    return <h2>Welcome, {this.props.name}!</h2>;
  }
}

// Usage
<Greeting name="Bob" />

Composing Components

Components can be composed together to build complex UIs. This is one of the key advantages of using components in React.

// Example of composing components
function App() {
  return (
    <div>
      <Welcome name="Alice" />
      <Greeting name="Bob" />
    </div>
  );
}

// Usage
<App />

State and Lifecycle

Functional Components with Hooks

Functional components can manage state using the useState hook, introduced in React 16.8.

// Example of a functional component with state
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Class Components

Class components manage state using the this.state property and lifecycle methods.

// Example of a class component with state and lifecycle
class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

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

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={this.incrementCount}>
          Click me
        </button>
      </div>
    );
  }
}

Best Practices

Use Functional Components When Possible

Functional components are simpler and easier to understand, especially with the introduction of hooks. They promote better code organization and reusability.

// Example of a functional component using hooks
import React, { useState } from 'react';

function TodoList() {
  const [todos, setTodos] = useState([]);

  return (
    <div>
      {/* Component implementation */}
    </div>
  );
}

Keep Components Small and Focused

Each component should have a single responsibility. This makes them easier to test, debug, and reuse.

// Example of a focused functional component
function TodoItem({ todo }) {
  return (
    <li>{todo.text}</li>
  );
}

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map(todo => <TodoItem key={todo.id} todo={todo} />)}
    </ul>
  );
}

Use PropTypes for Validation

PropTypes help ensure that components receive the correct types of props. This can prevent bugs and make your code more robust.

// Example of using PropTypes in a functional component
import React from 'react';
import PropTypes from 'prop-types';

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

Welcome.propTypes = {
  name: PropTypes.string.isRequired,
};

Memoize Pure Components

For components that only re-render when their props change, use React.memo to optimize performance.

// Example of memoizing a functional component
import React from 'react';

const MyComponent = React.memo(function MyComponent({ prop }) {
  // Component implementation
});

Conclusion

Components are the building blocks of React applications. Understanding how to create, manage, and compose components is crucial for developing efficient and maintainable UIs. By following best practices and leveraging modern features like hooks, you can build powerful and scalable React applications.

In the next section, we will explore more advanced topics such as component communication, context, and error handling. Keep practicing by building small projects and experimenting with different component structures to solidify your understanding.


PreviousJSX: Basics and SyntaxNext Functional Components

Recommended Gear

JSX: Basics and SyntaxFunctional Components