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



How to apply validation on props in reactjs? Apply validation in previously developed example in abo..

Answer / 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

More React.js Interview Questions

State the main difference between Pros and State?

1 Answers  


How can you share an element in the parsing?

1 Answers  


What do you understand by “single source of truth”?

1 Answers  


What is the lifecycle methods order in mounting?

1 Answers  


How to use forms in reactjs?

1 Answers  


How to set state with a dynamic key name?

1 Answers  


How to build the React app for production?

1 Answers  


What is reactjs-jsx? Explain basic code snippet of jsx with the help of a practical example?

1 Answers  


what is the usage of refs and keys used in react?

1 Answers  


Can you update props in react?

1 Answers  


State the difference between getIntialState() and constructor()?

1 Answers  


What is React?

1 Answers  


Categories