Skip to main content

React: Default Props

Table of Contents

Intro

Because we want our components to be as re-usable as possible there will be times where we want to set a default value to props. For example if we have a spinner component that simply covers the whole page with a spinning icon.

loading icon

Default Props

We can pass in the message below the spinning icon very easily using a prop called message. However if we have not explicitly passed in a message to this component, instead of having it be blank we can have a set of default props using the .defaultProps method.

import React from "react";

const Loading = (props) => {
return (
<div class="ui segment centering-container">
<div class="ui active dimmer">
<div class="ui text loader">{props.message}</div>
</div>
<p></p>
</div>
);
};

Loading.defaultProps = {
message: "Loading...",
};

export default Loading;

loading... message under icon

Comments

Recent Work

Free desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.

Learn More

BidBear

bidbear.io

Bidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.

Learn More