adspace


How to apply validation on props in reactjs? Apply validation in previously developed example in above question?

Answer Posted / Vijay Kumar

To apply validation on props in a React component, you can use a library like `prop-types`. Here's how you could modify the previous example to include prop validation:nn```javascriptnimport React from 'react';nimport PropTypes from 'prop-types';nnclass MyComponent extends React.Component {n static propTypes = {n name: PropTypes.string.isRequired,n age: PropTypes.number.isRequired,n }nn render() {n return (<div>n {this.props.name} is {this.props.age} years old.n </div>);n }n}n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How would you create higher order components (hocs) in react.js?

447


How to use events in reactjs? Give an example of using events?

559