From ba1db591d95d97bb18bd4bd34641d93732b138c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Wed, 17 Jul 2013 09:12:02 -0700 Subject: [PATCH] Write Default Props section --- docs/05-reusable-components.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/05-reusable-components.md b/docs/05-reusable-components.md index 3b2f9792..f628d0f2 100644 --- a/docs/05-reusable-components.md +++ b/docs/05-reusable-components.md @@ -17,6 +17,24 @@ As your app grows it's helpful to ensure that your components are used correctly ** TODO zpao ** +## Default Prop Values + +React let's you define default values for your `props` in a very declarative way: + +```javascript +var ComponentWithDefaultProps = React.createClass({ + getDefaultProps: function() { + return { + value: 'default value' + }; + } + /* ... */ +}); +``` + +The result of `getDefaultProps()` will be cached and used to ensure that `this.props.value` will have a value if it was not specified by the parent component`. This allows you to safely just use your props without having to write repetitive and fragile code to handle that yourself. + + ## Transferring Props: A Shortcut A common type of React component is one that extends a basic HTML in a simple way. Often you'll want to copy any HTML attributes passed to your component to the underlying HTML element to save typing. React provides `transferPropsTo()` to do just this.