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

5 / 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/Functional Components
⚛️React.js

Functional Components

Updated 2026-04-20
3 min read

Introduction to Functional Components

Functional components are one of the fundamental building blocks in React.js, offering a simpler and more concise way to create reusable UI elements. Introduced in React 16.8 with the addition of Hooks, functional components have become the preferred choice for most developers due to their simplicity and flexibility.

In this tutorial, we will explore what functional components are, how they differ from class components, and how to effectively use them in your React applications. We'll cover real-world examples, best practices, and advanced techniques to help you master functional components with Hooks.

What is a Functional Component?

A functional component is a JavaScript function that returns JSX (JavaScript XML), which describes what the UI should look like. This function takes props as an argument and can use these props to render dynamic content.

Here's a simple example of a functional component:

import React from 'react';

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

export default Greeting;

In this example, Greeting is a functional component that takes a name prop and returns an <h1> element with the greeting message.

Differences Between Functional and Class Components

Syntax

Functional Component:

function MyComponent({ message }) {
  return <div>{message}</div>;
}

Class Component:

import React, { Component } from 'react';

class MyComponent extends Component {
  render() {
    return <div>{this.props.message}</div>;
  }
}

Lifecycle Methods

  • Functional Components: Use Hooks (e.g., useEffect, useState) to handle lifecycle-like behavior.
  • Class Components: Have built-in lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount.

State Management

  • Functional Components: Use the useState hook to manage state.
  • Class Components: Manage state using the this.state object.

Using Functional Components in React Applications

Basic Usage

To use a functional component, you simply import it and include it in your JSX:

import React from 'react';
import ReactDOM from 'react-dom';
import Greeting from './Greeting';

function App() {
  return (
    <div>
      <h1>Welcome to My App</h1>
      <Greeting name="Alice" />
      <Greeting name="Bob" />
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('root'));

Props

Props (short for properties) are used to pass data from a parent component to a child component. In functional components, props are passed as arguments to the function:

function User({ name, email }) {
  return (
    <div>
      <h2>{name}</h2>
      <p>Email: {email}</p>
    </div>
  );
}

export default User;

Children Props

You can also pass children elements as props using the children prop:

function Layout({ header, footer, children }) {
  return (
    <div className="layout">
      <header>{header}</header>
      <main>{children}</main>
      <footer>{footer}</footer>
    </div>
  );
}

export default Layout;

Usage:

<Layout header={<h1>My Header</h1>} footer={<p>My Footer</p>}>
  <p>This is the main content.</p>
</Layout>

Best Practices for Functional Components

Use Descriptive Names

Always use descriptive and meaningful names for your components. This makes your code more readable and maintainable.

// Good
function UserProfile({ user }) {
  // component implementation
}

// Bad
function UserCard({ user }) {
  // component implementation
}

Keep Components Small and Focused

Each component should have a single responsibility. If a component is doing too much, consider breaking it down into smaller components.

// Good
function UserProfile({ user }) {
  return (
    <div>
      <UserProfileHeader user={user} />
      <UserProfileBody user={user} />
    </div>
  );
}

function UserProfileHeader({ user }) {
  // header implementation
}

function UserProfileBody({ user }) {
  // body implementation
}

Use PropTypes for Validation

While TypeScript can provide type safety, using PropTypes is a good practice to validate props in JavaScript projects.

import React from 'react';
import PropTypes from 'prop-types';

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

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

export default Greeting;

Avoid Inline Functions in Render

Inline functions can lead to performance issues due to unnecessary re-renders. Use the useCallback or useMemo hooks when necessary.

import React, { useCallback } from 'react';

function MyComponent({ onClick }) {
  const handleClick = useCallback(() => {
    // handle click logic
  }, [onClick]);

  return <button onClick={handleClick}>Click me</button>;
}

Advanced Techniques

Higher-Order Components (HOCs)

Higher-order components are functions that take a component and return a new component with additional functionality.

import React from 'react';

function withLogger(WrappedComponent) {
  function WithLogger(props) {
    console.log('Rendering', WrappedComponent.name);
    return <WrappedComponent {...props} />;
  }

  WithLogger.displayName = `WithLogger(${getDisplayName(WrappedComponent)})`;
  return WithLogger;
}

function getDisplayName(WrappedComponent) {
  return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}

Usage:

const EnhancedGreeting = withLogger(Greeting);

Render Props

Render props is a technique for sharing code between components using a prop whose value is a function.

import React, { useState } from 'react';

function MouseTracker({ render }) {
  const [mouse, setMouse] = useState({ x: 0, y: 0 });

  const handleMouseMove = (event) => {
    setMouse({
      x: event.clientX,
      y: event.clientY,
    });
  };

  return (
    <div style={{ height: '100vh' }} onMouseMove={handleMouseMove}>
      {render(mouse)}
    </div>
  );
}

function App() {
  return (
    <MouseTracker
      render={(mouse) => (
        <h1>
          The mouse position is ({mouse.x}, {mouse.y})
        </h1>
      )}
    />
  );
}

Conclusion

Functional components with Hooks are a powerful and flexible way to build React applications. They offer a simpler syntax, better performance, and easier state management. By following best practices and utilizing advanced techniques, you can create maintainable and efficient UI components.

In the next sections of this course, we will dive deeper into Hooks, Context API, and other advanced features that further enhance the capabilities of functional components in React.js.


PreviousComponents IntroductionNext Class Components

Recommended Gear

Components IntroductionClass Components