Frequently Asked Questions

React PropTypes: Concepts & Usage

What are React PropTypes and why are they important?

React PropTypes are a method for validating the types of props passed to React components. They help ensure that components receive the correct data types, reducing bugs and improving code reliability. PropTypes are checked only in development mode, providing validation and warnings in the browser console. Learn more.

How do you install PropTypes in a React project?

You can install PropTypes by running npm install prop-types --save in your project directory. After installation, import PropTypes into your component files to use its validation features. Source.

What types of PropTypes are available for validation?

PropTypes offers basic types (string, number, object, array, boolean, function, symbol), multiple types (using oneOf and oneOfType), collective types (arrayOf, objectOf, shape, exact), and advanced types like element for component validation. Source.

How do you make a prop required in React using PropTypes?

To make a prop required, append .isRequired to the PropType definition. If the required prop is missing, React will display a warning in the console during development. Source.

How can you validate arrays and objects with PropTypes?

Use PropTypes.arrayOf to validate arrays of a specific type and PropTypes.objectOf to validate objects with values of a specific type. PropTypes.shape and PropTypes.exact allow for more granular validation of object structures. Source.

How do you set default prop values in React?

Default prop values can be set using defaultProps on your component. This ensures that if a prop is not provided, the default value is used. Source.

Why use PropTypes instead of TypeScript?

PropTypes provide runtime validation in development mode, while TypeScript offers compile-time type checking. PropTypes are useful for quick validation and legacy projects, whereas TypeScript is preferred for larger codebases and stricter type enforcement. Source.

How do you create custom validators with PropTypes?

Custom validators are functions that receive props, propName, and componentName as arguments. You can use these to implement custom logic, such as regex validation for emails, and throw errors if validation fails. Source.

How can you validate that a prop is a React component?

Use PropTypes.element to validate that a prop is a React element. This is useful for ensuring that a component receives only valid children or elements. Source.

How do you validate multiple types for a single prop?

Use PropTypes.oneOfType to allow a prop to accept multiple data types, such as an array or object. This validator takes an array of PropTypes definitions. Source.

Where can I find a real-world example of PropTypes in use?

A real-world example is available in the cocktail app demo, which fetches content from Hygraph and validates props using PropTypes. You can also view the deployed project.

How do you validate strict object shapes with PropTypes?

Use PropTypes.exact to enforce strict object shapes, ensuring that only the specified keys are present in the object. Extra keys will trigger a warning. Source.

How can you validate limited values for a prop?

Use PropTypes.oneOf to restrict a prop to a set of predefined values. If a value outside the set is passed, a warning is displayed in the console. Source.

How do you validate nested object structures in React?

Use PropTypes.shape to validate nested object structures, specifying the type for each key within the object. This helps ensure data integrity for complex props. Source.

How can you validate that a prop is an array of a specific type?

Use PropTypes.arrayOf to validate that a prop is an array containing elements of a specific type, such as numbers or strings. Source.

How do you validate that a prop is an object with values of a specific type?

Use PropTypes.objectOf to validate that a prop is an object whose values are of a specific type, such as strings. Source.

How do you validate props in large React applications?

In large applications, PropTypes help teams avoid unexpected bugs by validating props and ensuring consistent data structures across components. This is especially useful when collaborating with multiple developers. Source.

How do you receive props in a child component?

Props can be received in a child component by destructuring them in the function definition or by using the props parameter. Source.

What is the difference between PropTypes and defaultProps?

PropTypes are used for validating the type and presence of props, while defaultProps are used to set default values for props if they are not provided. Source.

Where can I find more information about React PropTypes?

For more details, visit the React PropTypes Documentation and the Hygraph blog post on PropTypes.

Hygraph Features & Capabilities

What is Hygraph and what are its key capabilities?

Hygraph is a GraphQL-native Headless CMS designed to help businesses build, manage, and deliver digital experiences at scale. Key capabilities include content federation, Smart Edge Cache, custom roles, rich text management, project backups, and enterprise-grade security. Source.

How does Hygraph's Smart Edge Cache improve performance?

Smart Edge Cache enhances performance by accelerating content delivery and reducing latency, making Hygraph ideal for high-traffic and global audiences. Source.

What security and compliance certifications does Hygraph have?

Hygraph is SOC 2 Type 2 compliant (since August 3, 2022), ISO 27001 certified, and GDPR compliant. These certifications ensure robust security and adherence to international standards. Source.

What feedback have customers given about Hygraph's ease of use?

Customers praise Hygraph for its intuitive user interface, accessibility for non-technical users, and ease of setup. Hygraph was recognized for "Best Usability" in Summer 2023. Source.

How does Hygraph support enterprise-grade compliance?

Hygraph offers dedicated hosting, custom SLAs, granular permissions, SSO integrations, audit logs, encryption, and regular backups to meet enterprise compliance requirements. Source.

What are the key benefits of using Hygraph?

Hygraph delivers operational efficiency, cost reduction, scalability, technical flexibility, and proven results such as 3X faster time-to-market (Komax) and a 15% increase in customer engagement (Samsung). Source.

How does Hygraph's GraphQL API enhance developer experience?

Hygraph's GraphQL-native architecture simplifies schema evolution, data retrieval, and integration with third-party systems, making it easier for developers to build and maintain content-driven applications. Source.

What problems does Hygraph solve for businesses?

Hygraph addresses operational inefficiencies, financial challenges, and technical issues such as developer dependency, legacy tech stack modernization, content inconsistency, high costs, slow speed-to-market, integration difficulties, and performance bottlenecks. Source.

Who is the target audience for Hygraph?

Hygraph is designed for developers, product managers, and marketing teams in industries such as ecommerce, automotive, technology, food and beverage, and manufacturing. It is ideal for organizations seeking scalable, future-proof content management. Source.

How does Hygraph differentiate itself from competitors?

Hygraph stands out as the first GraphQL-native Headless CMS, offering content federation, Smart Edge Cache, user-friendly tools, and enterprise-grade features. It focuses on flexibility, scalability, and integration capabilities, setting it apart from platforms like Sanity, Prismic, and Contentful. Source.

What is the typical implementation timeline for Hygraph?

Implementation timelines vary by project. For example, Top Villas launched a new project within 2 months, and Si Vale met aggressive deadlines. Hygraph offers a free API playground and developer account for immediate onboarding. Source.

What resources are available for onboarding and training?

Hygraph provides structured onboarding, webinars, live streams, how-to videos, and extensive documentation to support new users. Documentation.

What customer success stories demonstrate Hygraph's impact?

Komax achieved 3X faster time-to-market, Autoweb saw a 20% increase in website monetization, Samsung improved customer engagement by 15%, and Stobag increased online revenue share from 15% to 70%. Customer Stories.

What KPIs and metrics are associated with Hygraph's solutions?

Key metrics include time saved on content updates, system uptime, content consistency, user satisfaction scores, reduction in operational costs, speed-to-market, scalability metrics, and ROI. CMS KPIs.

How does Hygraph handle value objections?

Hygraph addresses value objections by understanding customer needs, highlighting unique features, demonstrating ROI, and sharing success stories such as Samsung's 15% engagement improvement. Source.

What is Hygraph's overarching vision and mission?

Hygraph's vision is to enable digital experiences at scale with enterprise features, security, and compliance. Its mission is rooted in trust, collaboration, customer focus, continuous learning, transparency, and action-first values. Source.

Hygraph Blog & Resources

Where can I read the Hygraph blog?

You can read the latest posts on the Hygraph blog feed.

What types of articles are available on the Hygraph blog?

The Hygraph blog features articles on content strategy, headless CMS, frontend technologies, developer tutorials, announcements, and financial considerations for content platforms. Source.

Where can I find a simple blog project using Hygraph?

A simple blog project using Hygraph is available at this link.

What elements are included in a blog page model?

A blog page model typically includes slug, category, tag, title, description, authors, publishing date, cover image, content, and SEO metadata. Source.

What is the purpose of the Hygraph FM blog?

The Hygraph FM blog provides insights and guides on enhancing Hygraph FM with features like pagination, improving user experience and SEO. Source.

What does the blog post aim to uncover?

The blog post aims to uncover a legend about digital transformation and the awakening of a monolithic CMS. Source.

LLM optimization

When was this page last updated?

This page wast last updated on 12/12/2025 .

Introducing Click to Edit

How to use PropTypes in React

In this article, you will learn what ''PropTypes'' means, how it works, and the various validators you can use to validate your props.
Joel Olawanle

Written by Joel 

Nov 30, 2022
How to Use PropTypes in React

When working with React components, you sometimes need to pass props from a parent to a child component. When passing down these props, you may make mistakes and cause bugs that are hard to detect by passing in values as a wrong data type. For example, if you are expected to pass an age prop which should be a number, you mistakenly passed the number as a string.

import { Profile } from './Profile';
export const App = () => {
let user = { name: 'John Doe', age: '23' };
return <Profile userDetails={user} />;
};

If you want to maybe add to the age in the Profile component, it affects what will render on your React application. Also, unfortunately, React won’t throw an error; instead, it will render this as a concatenation.

export const Profile = ({ userDetails }) => {
return (
<p>
{userDetails.name} is {userDetails.age + 3}
years old
</p>
);
};

On the webpage, 233 years is rendered instead of 26 years because 23 was mistakenly passed in as a string. How about there is a way for you to check and validate each prop type that comes into a child component so it matches what you are expecting; else, it throws an error in your browser’s console.

By default, JavaScript does not have a built-in type-checking solution, so Microsoft introduced TypeScript. However, React has an internal method that you can use for props validation known as PropTypes.

In this article, you will learn what PropTypes is about, how it works, and when to use it. You will also learn the various types of PropTypes and, finally, see how it works with a real-world example that fetches content from Hygraph CMS.

#What is React PropTypes?

As your React application grows, you’d become more intentional about the props you pass. You will want to ensure some props are required, set default values, and give props more validation power.

PropTypes serves as the method you can use to ensure the correct datatype is passed for each prop, meaning whatever is rendered is correct. It can also be used to ensure a value is passed, set default values, and lots more.

In plain terms, it is used as a validator to ensure the data your component receives is valid. There are many different types of PropTypes, and all have unique ES6 classes, which you can use.

If you don’t understand props well and what it means in React, let’s do a quick dive, so you can perfectly understand this article.

#Understanding Props in React

A prop is a short form for “properties”. You can use it to pass down data from a parent component to a child component. For example, if you fetch a list of products from an external in the App.js component, you also want to use these same products in a child component. Instead of having to fetch or pass the data again, you can pass down the data easily.

import { Profile } from './Profile';
export const App = () => {
let name = "John Doe";
let age = 50;
return <Profile name={name} age={age} userName="Jonny" />;
};

This data can be received in the child component by destructuring or using the props parameter:

export const Profile = (props) => {
return (
<p>
{props.name} is {props.age + 3}
years old
</p>
);
};

Props can receive data in different datatypes such as arrays, objects, strings, numbers, and lots more.

#How and when should you use PropTypes

When working with functions, you would pass in arguments, which, when these arguments are not passed, will throw an error or break your application. This also applies to React component; you will sometimes want to pass required prop(s) or validate the datatype of each prop because failure to pass in the correct prop will affect your application’s behavior. You can do all these with PropTypes.

Also, when working on large applications for clients and with a team, it is always good to validate these props to avoid unexpected production bugs.

How to use PropTypes

Previously, before the release of React v15.5, PropTypes was inbuilt into the React package, but now it has been moved into a different package which you will first have to install into your project to make use of it.

You can install it in your project by running the following command in your terminal:

$ npm install prop-types --save

Once it has been installed, you will first have to import it into whichever component you want to use to access its features.

import PropTypes from 'prop-types';

When you import it into your component, you can now attach the component name to the PropTypes method and create an object containing all your prop's validation.

import PropTypes from 'prop-types';
export const Profile = (props) => {
return (
<p>
{props.name} is {props.age + 3}
years old
</p>
);
};
Profile.propTypes = {
// Props validations here
};

Your PropTypes object will contain key-value pairs for each prop you validate, the key is the prop's name, and the value is the PropTypes validation (the type and other validation). Let's now explore the various Prop types with examples.

#Types of PropTypes

The PropTypes method holds a wide range of classes that you can use to validate and configure type definitions. These types will be grouped into sections, so they are easy to understand.

Basic types

The basic types are the regular data types you always use in JavaScript. This includes string, number, object, array, boolean, function, and symbol. You can only use them by attaching the PropTypes method.

ComponentName.propTypes = {
anyValue: PropTypes.any, // the prop can be of any data type
myString: PropTypes.string, // prop should be a string
myNumber: PropTypes.number, // prop should be a number
myObject: PropTypes.object, // prop should be an object
myArray: PropTypes.array, // prop should be an array
myBoolean: PropTypes.bool, // prop should be boolean
myFunction: PropTypes.func, // prop should be a function
mySymbol: PropTypes.symbol, // prop should be a symbol
};

For example, if you pass three props which are name, age, and isLoggedIn, then you can configure their types:

import PropTypes from 'prop-types';
export const Profile = ({ name, age, isloggedIn }) => {
return (
<>
{isloggedIn && (
<p>
{name} is {age + 3}
years old
</p>
)}
</>
);
};
Profile.propTypes = {
name: PropTypes.string,
age: PropTypes.number,
isloggedIn: PropTypes.bool,
};

You can make a prop required by attaching .isRequired, meaning when the prop is not passed, it will throw an error in our console.

Profile.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired,
isloggedIn: PropTypes.bool.isRequired,
};

When you don’t pass a particular prop, you will get an error like this in your console:

failed prop type error

Multiple types

PropTypes can also allow you to set the type of values your props should be and the expected value. It allows you to set a limited set of values or multiple data types for a prop. This can be done using PropTypes.oneOf() and PropTypes.oneOfType() validators. These validators take in an array.

  • PropTypes.oneOf(): This validator can be used to set limited values for a prop. For example, if you want the value of your name prop to be either “John” or “Jane”. You can achieve this with this validator.
Profile.propTypes = {
name: PropTypes.oneOf(['John', 'Jane']).isRequired,
// ...
};

This means when you pass in another value asides from “John” and “Jane” to the name prop, it will throw an error:

invalid prop type error

  • PropTypes.oneOfType(): Just like you can set limited values for your prop, you can also set multiple datatypes for your prop, meaning if you want a prop value to either be an array or object, you can set it with this validator.
Profile.propTypes = {
// ...
arrayOrObject: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
};

Passing a value that may be a string to the arrayOrObject prop will throw an error to your console.

Collective types

You now know how these PropTypes work, and you have seen the basic types, similar to the regular data types in programming. Some other types help you validate more advanced types of props.

Array

Asides from PropTypes.array validator, which you can use for array types, PropTypes uses the PropTypes.arrayOf() validator to perform more fine-tuned validation of arrays. This method allows you to check the type of values in an array.

For example, your component might expect a prop with an array of numbers, and then you can use PropTypes.arrayOf() to validate only numbers.

Profile.propTypes = {
// ...
myArray: PropTypes.arrayOf(PropTypes.number).isRequired,
};

If a typo error occurs, maybe a number is passed as a string, then this validator will throw an error.

invalid prop type - typo error

Object

Just as you have learned with Arrays, you can also use the PropTypes.objectOf() validator to check the type of values in an object.

Profile.propTypes = {
// ...
myObject: PropTypes.objectOf(PropTypes.string).isRequired,
};

In the code above, myObject prop will only accept an object with values that are string, such as:

myObject: {
pet: 'cat',
food: 'pizza',
},

When you maybe have a number as its value or introduce a new key-value pair such as age: 10, it will throw an error in your console.

Two other validators work with objects. These validators help you validate the type for each value in an object and can also help make the object closed, meaning when you add another key-value pair whose type is not declared it will throw an error. These two validators are:

  • PropTypes.shape(): This can be used to define a set of keys for an object and validate its data types. For example, if an object consists of name, age, and id, then you can validate the type for each key.
Profile.propTypes = {
userDetails: PropTypes.shape({
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired,
}),
};

This means that when you pass a string as the age value, it will throw an error. When you define all these validations for each key in an object, you will notice that when you define a new key-value pair, you can use it within your application without validating its type. You can fix this using the PropTypes.exact() validator.

  • PropTypes.exact(): This validator can be used for strict or exact object matching. This means that it will throw an error when you have extra properties in the object you are validating:
Profile.propTypes = {
userDetails: PropTypes.exact({
id: PropTypes.number,
name: PropTypes.string,
age: PropTypes.number,
}).isRequired,
};

Advanced type checking for components and component names

In some scenarios, you might have to pass in a component as a prop. To check if a prop is a component, you can use the PropTypes.element validator.

Component.propTypes = {
elementProp: PropTypes.element
}

One significant advantage and common use for the PropTypes.element validator is to ensure that a component has only one child. If the component has no children or multiple children, it will display a warning on the JavaScript console.

Component.propTypes = {
children: PropTypes.element.isRequired
}

Custom types

You have learned how to validate props with the default validators, but how about creating your validators. Suppose you want to validate the value of a prop to be sure it matches what you expect— maybe an email value.

You can do this by creating a custom validator. To create a custom validator, you will create a function that can take in three arguments which are:

  • props: The original object containing all the props passed to the component
  • propName: The name of the prop you want to validate
  • componentName: The name of the component

In the function, you can maybe use a regex to test if the value is an actual email, check for the length, and lots more, and then throw an error if the validation fails using new Error().

Profile.propTypes = {
userDetails: PropTypes.shape({
// ...
email: function (props, propName, componentName) {
const regex = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/g;
if (!regex.test(props[propName])) {
return new Error(
`The prop ${propName} passed to ${componentName}is not a valid email address.`
);
}
},
}),
};

When you pass in a wrong email value, it will throw the error. You can also decide to extract the validator into a function, so your propTypes object is neat and easy to maintain.

const isEmail = function (props, propName, componentName) {
const regex = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/g;
if (!regex.test(props[propName])) {
return new Error(
`The prop ${propName} passed to ${componentName}is not a valid email address.`
);
}
};
Profile.propTypes = {
userDetails: PropTypes.shape({
id: PropTypes.number,
name: PropTypes.string,
age: PropTypes.number,
email: isEmail,
}),
};

Setting the default prop value

So far, you have learned how to validate props, configure PropTypes, and lots more. It is also important to know that you can set a default value for each prop. You can do this similarly to how PropTypes work but this time, you would use defaultProps (there is no need to import it).

Profile.defaultProps = {
userDetails: {
id: 1,
name: 'John Doe',
age: 30,
},
};

#React PropTypes example

Let’s now use PropTypes validators in a real-life project to see how it works. Here is the source code for the project (a cocktail app that fetches content from Hygraph). On the Products.jsx page, which lists all available cocktails, the array is looped, and each object is passed as a prop to the Product.Jsx component.

On the Product.jsx component, the object is validated with the PropTypes.object.

import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
const Product = ({ product }) => {
return (
<div key={product.id} className="cocktail-card">
<img src={product.image.url} alt="" className="cocktail-img" />
<div className="cocktail-info">
<div className="content-text">
<h2 className="cocktail-name">{product.name}</h2>
<span className="info">{product.info}</span>
</div>
<Link to={`/products/${product.slug}`}>
<div className="btn">View Details</div>
</Link>
</div>
</div>
)
}
Product.propTypes = {
product: PropTypes.object.isRequired
}
export default Product

Also, on the ProductDetails.jsx page, you will notice an object is passed into the CocktailInfo.jsx component, where each object value is validated using PropTypes.shape().

import PropTypes from 'prop-types';
const CocktailInfo = ({ productInfo }) => {
return (
<div className="cocktail-infos">
<div className="row">
<h3 className="label">Name: </h3>
<p className="text">{productInfo.name}</p>
</div>
<div className="row">
<h3 className="label">Category: </h3>
<p className="text">{productInfo.category}</p>
</div>
<div className="row">
<h3 className="label">Info: </h3>
<p className="text">{productInfo.info}</p>
</div>
<div className="row">
<h3 className="label">Instructions: </h3>
<p className="text">{productInfo.instructions}</p>
</div>
<div className="row">
<h3 className="label">Ingredients: </h3>
<p className="text">{productInfo.ingredients}</p>
</div>
</div>
)
}
CocktailInfo.propTypes = {
productInfo: PropTypes.shape({
name: PropTypes.string,
category: PropTypes.string,
info: PropTypes.string,
ingredients: PropTypes.string,
instructions: PropTypes.string
})
}
export default CocktailInfo

You can check the GitHub repository to cross-check the code and the deployed project here.

#Conclusion

In this article, you have learned what PropTypes means, how it works, and the various validators you can use to validate your props.

Have fun coding!

Blog Author

Joel Olawanle

Joel Olawanle

Joel Olawanle is a Frontend Engineer and Technical writer based in Nigeria who is interested in making the web accessible to everyone by always looking for ways to give back to the tech community. He has a love for community building and open source.

Share with others

Sign up for our newsletter!

Be the first to know about releases and industry news and insights.