How are default props declared in a React functional component

0 votes
Can you tell me How are default props declared in a React functional component?
Feb 12 in Node-js by Nidhi
• 11,580 points
58 views

1 answer to this question.

0 votes

In React functional components, default prop values can be assigned using the following method:

Setting defaultProps as a Property of the Component:

Although this method is traditionally used, it is important to note that as of React 17, setting defaultProps on functional components is deprecated. Therefore, it is recommended to use default parameters instead.

import React from 'react';

const MyComponent = (props) => {

  return (

    <div>

      <p>{props.prop1}</p>

      <p>{props.prop2}</p>

    </div>

  );

};

MyComponent.defaultProps = {

  prop1: 'defaultValue1',

  prop2: 'defaultValue2',

};

export default MyComponent;
answered Feb 21 by kavya

Related Questions In Node-js

0 votes
1 answer

How can a child component's props be overwritten in React?

You can utilize the React.cloneElement function. This ...READ MORE

answered Feb 23 in Node-js by Kavya
101 views
0 votes
1 answer
0 votes
1 answer

How to Handle Jest Unit Testing for 'ɵcmp' in a React-in-Angular Hybrid App?

Encountering the 'ɵcmp' property error during Jest ...READ MORE

answered Dec 23, 2024 in Node-js by Navya
90 views
0 votes
1 answer

How do I create a custom popover in React?

Create a custom popover in React by ...READ MORE

answered Feb 23 in Node-js by Kavya
97 views
0 votes
1 answer

What distinguishes the loadingIndicatorSource prop from the defaultSource prop in React Native?

Property loadingIndicatorSource defaultSource Purpose Placeholder during remote image loading. Placeholder before load ...READ MORE

answered Feb 21 in Node-js by kavya
69 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP