React.js / React Components and Props

Creating Functional and Class Components

In this tutorial, you will learn about the two types of components in React: functional and class components. You'll learn how to create both types and understand their difference…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explores functional and class components, props, and component reusability.

Creating Functional and Class Components in React

1. Introduction

In this tutorial, we'll explore the world of React.js components. We'll look into the two primary ways of creating components in React: functional components and class components. We'll walk through the creation of each type and discuss their differences.

By the end of this tutorial, you'll be able to:

  • Understand what functional and class components are.
  • Create your own functional and class components.
  • Understand when to use each type of component.

Prerequisites:

  • Basic understanding of JavaScript and React.js.
  • A local development environment for React.js.

2. Step-by-Step Guide

Functional Components

Functional components are JavaScript functions. They can either be declared with function declaration or as an arrow function.

Here's an example of a simple functional component:

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

Class Components

Class components are ES6 classes. They should always extend React.Component and have a render method.

Here's an example of a simple class component:

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

3. Code Examples

Here are some practical examples for better understanding:

Code Example 1: Functional Component

// This is a functional component
function Welcome(props) {
  // We return some JSX that we want this component to render
  return <h1>Hello, {props.name}</h1>;
}

In this example, Welcome is a functional component. We pass props to it and use these props to output dynamic content within the JSX that we return.

Code Example 2: Class Component

// This is a class component
class Welcome extends React.Component {
  render() {
    // We return some JSX that we want this component to render
    return <h1>Hello, {this.props.name}</h1>;
  }
}

In this example, Welcome is a class component. It extends React.Component, so we have access to this.props in the render method.

4. Summary

  • A functional component is just a plain JavaScript function that accepts props as an argument and returns a React element.
  • A class component requires you to extend from React.Component and create a render function which returns a React element.

Next steps would be to delve deeper into the lifecycle methods of class components and the useState and useEffect hooks in functional components.

5. Practice Exercises

  1. Create a functional component named 'Greeting' that accepts a prop 'name' and renders a greeting message.

Solution:

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}
  1. Convert the above functional component into a class component.

Solution:

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

Continue practicing by creating more complex components with multiple props and different types of data.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help