React.js getDefaultProps & propTypes by Dan on CodePen

Using propTypes & getDefaultProps in #React:  #Coding

  • export default React-createClass({ getDefaultProps: function() { return { breakpoint: 500 }; }, propTypes: { breakpoint: React.
  • export default React-createClass({ getDefaultProps: function() { return { breakpoint: 500 }; }, render: function() {
    Whatever goes here

    } });

  • You can set breakpoint as a default prop in your component at, for example, 500px to make sure your code operates as desired.
  • GetDefaultProps allows you to give your component some props which can be overridden, but in case none are given to the component somewhere else in your code, you’ll have some defaults in there.
  • A way to set some default props for a component while being able to easily override them if need be.

So maybe it’s a relatively obscure React lifestyle event, but I finally found a practical use for getDefaultProps, so that’s cool.

@danielbuda: Using propTypes & getDefaultProps in #React: #Coding

, so that’s cool.

allows you to give your component some props which can be overridden, but in case none are given to the component somewhere else in your code, you’ll have some defaults in there.

as a default prop in your component at, for example, 500px to make sure your code operates as desired.

and pass it in as a prop which will override the default setting.

Setting a default breakpoint of 500:

And then on the parent component, after importing the component above (lets call it “Child”) you can pass in props to override the default:

and more.

So there it is. A way to set some default props for a component while being able to easily override them if need be.

React.js getDefaultProps & propTypes by Dan on CodePen