Home > OS >  React FC - Is declaring props as optional with an undefined default/fallback value considered superf
React FC - Is declaring props as optional with an undefined default/fallback value considered superf

Time:01-01

Imagine this component

function AudioPlayer({
   uri,
   volume = 1.0,
   onPlay = undefined <--- superfluous?
}) {

}

AudioPlayer.propTypes = {
   uri: PropTypes.string.isRequired,
   volume: PropTypes.number,
   onPlay: PropTypes.func,
}

As you can see, onPlay is optional (as declared in the prop-types), but... is it correct (I mean, not superfluous) to set it as onPlay = undefined?

Or should I just do:

function AudioPlayer({
   uri,
   volume = 1.0,
   onPlay,
}) {

}

CodePudding user response:

In these type of situations, I don't declare it at all. If I want to declare it just to show that this will exist in future, I assign a default value type of what it will be. Like 0 for number, or empty string for type of string etc.

  • Related