PropTypes

PropTypes in React are used to check the value of a prop which is passed into the component. These help in error hanling and are very useful in large scale applications.

Primitive Data Types 

Type Class/Syntax Example
String PropTypes.string “Geeks”
Object PropType.object {course: “DSA”}
Number PropType.number 15,
Boolean PropType.bool true
Function PropType.func const GFG ={return “Hello”}
Symbol PropType.symbol Symbol(“symbole_here”

Array Types

Type Class/Syntax Example
Array PropTypes.array []
Array of strings PropTypes.arrayOf([type]) [15,16,17]
Array of numbers PropTypes.oneOf([arr]) [“Geeks”, “For”, “Geeks”
Array of objects PropTypes.oneOfType([types]) PropTypes.instanceOf()

Object Types

Type Class/Syntax Example
Object PropTypes.object() {course: “DSA”}
Number Object PropTypes.objectOf() {id: 25}
Object Shape PropTypes.shape()

{course: PropTypes.string,
price: PropTypes.number}

Instance PropTypes.objectOf() new obj()

Javascript




import PropTypes from 'prop-types';
import React from 'react';
import ReactDOM from 'react-dom/client';
 
// Component
class ComponentExample extends React.Component{
    render(){
        return(
                <div>
                 
                    {/* printing all props */}
                    <h1>
                        {this.props.arrayProp}
                        <br />
 
                        {this.props.stringProp}
                        <br />
 
                        {this.props.numberProp}
                        <br />
 
                        {this.props.boolProp}
                        <br />
                    </h1>
                </div>
            );
    }
}
 
// Validating prop types
ComponentExample.propTypes = {
    arrayProp: PropTypes.array,
    stringProp: PropTypes.string,
    numberProp: PropTypes.number,
    boolProp: PropTypes.bool,
}
 
// Creating default props
ComponentExample.defaultProps = {
 
    arrayProp: ['Ram', 'Shyam', 'Raghav'],
    stringProp: "w3wiki",
    numberProp: "10",
    boolProp: true,
}
 
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
    <ComponentExample />
</React.StrictMode>
);


React Cheat Sheet

React is an open-source JavaScript library used to create user interfaces in a declarative and efficient way. It is a component-based front-end library responsible only for the view layer of a Model View Controller (MVC) architecture. React is used to create modular user interfaces and promotes the development of reusable UI components that display dynamic data.

React Cheat Sheet provides you with a simple, quick reference to commonly used React methods. All the important components and methods are provided in this single page

Table of Content

  • React Elements
  • ReactJS Import and Export
  • React Components
  • Lifecycle of Components
  • Conditional Rendering
  • React Lists
  • React DOM Events
  • React Hooks
  • PropTypes

Boilerplate: Follow the below steps to create a boilerplate

Step 1: Create the application using the command

npx create-react-app <<Project_Name>>

Step 2: Navigate to the folder using the command

cd <<Project_Name>>

Step 3: Open the App.js file and write the below code

Javascript




// App.js
 
import React from 'react';
import './App.css';
export default function App() {
    return (
        <div >
            Hello Geeks
            Lets start learning React
        </div>
    )
}


Similar Reads

JSX

...

React Elements

JSX stands for JavaScript XML. JSX is basically a syntax extension of JavaScript. It helps us to write HTML in JavaScript and forms the basis of React Development. Using JSX is not compulsory but it is highly recommended for programming in React as it makes the development process easier as the code becomes easy to write and read....

ReactJS Import and Export

React elements are different from DOM elements as React elements are simple JavaScript objects and are efficient to create. React elements are the building blocks of any React app and should not be confused with React components....

React Components

In ReactJS we use importing and exporting to import already created modules and export our own components and modules rescpectively...

Lifecycle of Components

A Component is one of the core building blocks of React. Components in React basically return a piece of JSX code that tells what should be rendered on the screen....

Conditional Rendering

...

React Lists

...

React DOM Events

The lifecycle methods in ReactJS are used to control the components at different stages from initialization till unmounting....

React Hooks

...

PropTypes

In React, conditional rendering is used to render components based on some conditions. If the condition is satisfied then only the component will be rendered. This helps in encapsulation as the user is allowed to see only the desired component and nothing else....

Error Boundaries

...