Languages
[Edit]
EN

React - default property values in component

6 points
Created by:
p_agon
589

In this article, we would like to show you how to set default property values in React component.

Quick solution:

 

There are available two approaches how to do it, so check the below examples:

1. Using default properties

Edit

This approach is the most common and currently recommended (ES6). We are using here assigning default values to the properties of a function component.

Runnable example:

2. Using defaultProps

Edit

Instead, this approach uses the built-in defaultProps functionality, which is less recommended.

Note:

According to the developers of React, this functionality will be discontinued.

Runnable example: 

Note:

Main disadvantage of that solution is necesssity to add to each component additional code that generates overhead and effort for programmers. 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - default property values in component
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join