How React handle or restrict Props to certain types, or require certain Props to exist ? Last Updated : 24 Nov, 2023 Comments Improve Suggest changes Like Article Like Report In React, data can be transmitted from a parent component to its children using props. PropTypes, a feature in React, facilitates runtime type checks on these props, issuing warnings in the browser console if the checks fail. This ensures data consistency and helps catch potential issues during development. Syntax: import PropTypes from 'prop-types';ComponentName.propTypes = { name: PropTypes.any.isRequired};Steps to Create the React Application And Installing Module:Step 1: Create a new react project using the following command on the command line. npx create-react-app foldernameStep 2: Change your directory to the newly created folder by using the following command. cd foldernameProject Structure: Approach:App Component Structure:In `App.js`, the `App` component includes the `Info` component as its child, passing two props: `name` (Number type) and `username` (String type).Info Component Definition:In `Info.js`, the `Info` functional component is defined, receiving props. Prop types are employed to enforce type checks, ensuring that `name` is of type number and `username` is of type string, with the latter being required.Type Check Implementation:Prop types are used to enforce strict type checking in the `Info` component, preventing runtime errors by specifying that `name` must be a number and `username` must be a required string.Console Warning for Missing Prop:Failure to provide the required `username` prop to the `Info` component results in a warning in the browser console, emphasizing the necessity of fulfilling prop requirements.Example: Below the code of above explained approach. JavaScript //App.js import Info from './Info'; const App = () => { return <Info name={1} username={'gfg@123'} />; }; export default App; JavaScript //Info.js import PropTypes from 'prop-types'; const Info = (props) => { return ( <div> <div>Name: {props.name}</div> <div>Username: {props.username}</div> </div> ); }; Info.propTypes = { name: PropTypes.string, username: PropTypes.string.isRequired, }; export default Info; Step to Run the application: To start your application, use the following command on the command line. npm startOutput: Open the browser and go to http://localhost:3000 Comment More infoAdvertise with us Next Article How React handle or restrict Props to certain types, or require certain Props to exist ? S shivamsingh00141 Follow Improve Article Tags : Web Technologies ReactJS React-Questions Similar Reads How to Reference a Specific Object to Pass Down as Properties in ReactJS ? ReactJS, a widely used JavaScript library, employs Props, enabling the passing of data from parent to child components. Props are read-only, facilitating data access by child components via 'this.props.' The ReactJS library has two components: Functional ComponentClass ComponentSteps to Create the R 3 min read How to use PropTypes for Type-Checking in React Components ? React is a JavaScript library for building user interfaces, and one of its key features is component-based architecture. When building React components, it's essential to ensure that the data passed to them is of the correct type. PropTypes is a built-in type-checking library in React that helps dev 7 min read How to Restrict a Generic Interface from Accepting an Unknown Data Type ? Restricting a generic interface from accepting an unknown data type means specifying constraints on the type parameter to disallow the unknown. This ensures that the interface can only be instantiated with types that are more specific than unknown, enhancing type safety and preventing unintended usa 2 min read How Do You Pass Props to `{this.props.children}`? Passing props is important for sharing data and functionality between parent and child components, facilitating component reusability and maintaining a clear separation of concerns in React applications. In this article, we will explore two different approaches to Pass Props to {this.props.children} 4 min read How to Extend the Possible Types of Values of Dictionary in TypeScript ? A dictionary in TypeScript is a data type that can store the data in the form of key-value pairs. The dictionaries in TypeScript can be created using the type keyword, Interfaces, or simple objects. The type of the values in a dictionary can be predefined in TypeScript which can be extended to store 3 min read Like