Skip to main content

Command Palette

Search for a command to run...

How to Use React Components – Props, Default Props, and PropTypes Explained ?

Published
2 min read
How to Use React Components – Props, Default Props, and PropTypes Explained ?
M

Hi, I'm Mushahir Mohamed, a passionate web developer with over 1 years of experience in building responsive and user-friendly websites and web applications. I specialize in front-end development using modern technologies such as HTML5, CSS3, JavaScript, and React.js and backend technology such as PHP Mysql.

Introduction:

React Components are the fundamental building blocks of React applications. They allow you to create reusable pieces of your user interface. Here's a simplified explanation of how to use them:

1.Functional Components:

  • Functional components are the simplest type of React components.

  • They are functions that return JSX to describe what should be displayed.

Example of creating and using a functional component:

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

// Usage:
<Greeting name="John" />

2. Class-based Components:

  • Class-based components are defined as ES6 classes and extend React. Component.

  • They have a render method where you return JSX to define the component's output.

Example of creating and using a class-based component:

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

// Usage:
<Welcome name="Sarah" />

3.props

Props (short for properties) are a way to pass data from a parent component to a child component. They are like function arguments for your components. You access props using this. props in class-based components or as function arguments in functional components. In the examples above, the name prop is used to customize the greeting message

import React from 'react';

// Functional Component
function Greeting(props) {
  return (
    <div>
      <h1>Hello, {props.name}!</h1>
      <p>Your age is {props.age} years old.</p>
    </div>
  );
}

// Usage of the Greeting component with props
function App() {
  return (
    <div>
      <Greeting name="John" age={30} />
      <Greeting name="Sarah" age={25} />
    </div>
  );
}

export default App;

Default Props:

Default props provide fallback values for props in case they are not provided. You can set default props for a component to ensure it always has the necessary data:

Greeting.defaultProps = {
  name: "Guest"
};

PropTypes:

PropTypes are used to specify the type of data that a prop should contain. They help catch and prevent runtime errors caused by incorrect data types. You can define PropTypes for a component like this:

import PropTypes from 'prop-types';

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

Remember to install the prop-types package if you haven't already:

npm install prop-types

conclusion

React components, whether functional or class-based, allow you to create reusable pieces of your UI. Props enable you to pass data between components, and you can set default props and use PropTypes for improved error handling and validation. These concepts are essential for building robust React applications.