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, 2025 in Node-js by Nidhi
• 16,260 points
651 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, 2025 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, 2025 in Node-js by Kavya
562 views
0 votes
1 answer
0 votes
1 answer

How can you implement a private route that requires authentication before rendering a component in React Router?

Basic Private Route Implementation (v6) import { Navigate, ...READ MORE

answered Apr 17, 2025 in Node-js by anonymous
573 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, 2025 in Node-js by kavya
552 views
0 votes
1 answer
0 votes
1 answer

How can a:hover be implemented using inline CSS styles in React?

In React, inline styles do not support ...READ MORE

answered Feb 21, 2025 in CSS by kavya
1,293 views
0 votes
1 answer
0 votes
1 answer

How to use props in a Class-based Component to display data?

In a class-based React component, you can ...READ MORE

answered Mar 24, 2025 in Node-js by anonymous
601 views
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