@samitouri / QOS-React-2 / commits / 35f092afef
Fork 0

The library for web and native user interfaces.

Write Default Props section

Paul O’Shannessy committed Jul 17, 2013 at 09:12 UTC 35f092afef1d587cf3d272998f31a925b3d079e9
1 file changed +18
docs/docs/05-reusable-components.md
+18
@@ -17,6 +17,24 @@ As your app grows it's helpful to ensure that your components are used correctly
17 ** TODO zpao **
18
19
20 +## Default Prop Values
21 +
22 +React let's you define default values for your `props` in a very declarative way:
23 +
24 +```javascript
25 +var ComponentWithDefaultProps = React.createClass({
26 + getDefaultProps: function() {
27 + return {
28 + value: 'default value'
29 + };
30 + }
31 + /* ... */
32 +});
33 +```
34 +
35 +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.
36 +
37 +
38 ## Transferring Props: A Shortcut
39
40 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.