1

I have a field score which is required. I want to provide a default value for it as a fallback, so the warning will be displayed in console but the app will work as normal meanwhile.

Score.defaultProps = {
  dot  : false,
  score: 100
}

Score.propTypes = {
  score: PropTypes.number.isRequired,
  dot  : PropTypes.bool
}

If I remove the the default value, it works as expected.

I'm in Storybook and I need to display a table with PropTypes as follows:

expected output in Storybook

current output with default score

  • It's either required or it's not, there's no in between, something is either true or false. You can make the field to be non-required and still make your message appear. – whatamidoingwithmylife Feb 27 '19 at 17:07
  • The field is required, but I don't want the layout to break if no score is provided. It is just as a fallback. – Timofte Elisei Feb 27 '19 at 17:11
  • If the `score` field is required, how could someone NOT provide it? It doesn't make sense o.O – user3210641 Feb 27 '19 at 17:21
  • If you do not provide the score it will still render something on the page (not necessary well-formed), and will display an error in the browser console. My aim is to have the warn in the console and a default value in case of a missing score. Is this an anti-pattern? – Timofte Elisei Feb 27 '19 at 17:25
  • If all above comments make no sense, do two things - #1 remove isRequired. #2 update default score to say 9999. This way you could achieve two things - if score is not provided, things will not break and you could easily find out scores that are not provided (by looking score of 9999). – pritam Mar 26 '19 at 12:28

0 Answers0