DEV Community

Lam Hoang
Lam Hoang

Posted on

1

Type checking props with PropTypes in React

Use PropTypes

import PropTypes from 'prop-types';

Prop is an optional array

MyComponent.propTypes = {
  optionalArray: PropTypes.array,
};

Prop is an optional boolean

MyComponent.propTypes = {
  optionalBool: PropTypes.bool,
};

Prop is an optional function

MyComponent.propTypes = {
  optionalFunc: PropTypes.func,
};

Prop is an optional number (integer, float…)

MyComponent.propTypes = {
  optionalNumber: PropTypes.number,
};

Prop is an optional object

MyComponent.propTypes = {
  optionalObject: PropTypes.object,
};

Prop is an optional string

MyComponent.propTypes = {
  optionalString: PropTypes.string
};

Prop is an optional symbol

MyComponent.propTypes = {
  optionalSymbol: PropTypes.symbol,
};

Prop is an optional node (numbers, strings, elements, array, fragment)

MyComponent.propTypes = {   optionalNode: PropTypes.node, };

Source: React Cheat Sheet

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay