Skip to content

Commit 04a1166

Browse files
author
Vlad Balin
committed
Merge pull request #11 from Volicon/f/proptypes
F proptypes
2 parents 9b3c108 + f126ee6 commit 04a1166

File tree

7 files changed

+265
-54
lines changed

7 files changed

+265
-54
lines changed

README.md

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var React = require( 'nestedreact' );
126126
var MyComponent = React.createClass({
127127
//Model : BackboneModel,
128128
129-
attributes : { // Model defaults
129+
state : { // Model defaults
130130
count : 0
131131
},
132132
@@ -144,9 +144,9 @@ var MyComponent = React.createClass({
144144
});
145145
```
146146

147-
- New `NestedTypes` Model definition will be created, using `attributes` as Model.defaults.
147+
- New `NestedTypes` Model definition will be created, using `state` as Model.defaults.
148148
- If Model property is specified, it will be used as base model and extended.
149-
- `attributes` property from mixins will be properly merged.
149+
- `state` property from mixins will be properly merged.
150150
- Since `state` is `NestedTypes` model in this case,
151151
- All attributes *must* be declared using `NestedTypes` standard type specs.
152152
- `state` attributes allows direct assignments - treat it as regular object.
@@ -177,14 +177,52 @@ var MyComponent = React.createClass({
177177
You can update react component on backbone events from component props.
178178
Event subscription is managed automatically. No props passed - no problems.
179179

180+
## NestedTypes-style props specs
181+
182+
```javscript
183+
var MyComponent = React.createClass({
184+
props : {
185+
model : MyFancyModel
186+
},
187+
188+
listenToProps : { // or just string with property names, separated by space
189+
model : 'change'
190+
},
191+
192+
render : function(){
193+
return (
194+
<div onClick={ this.onClick }>
195+
{ this.props.model.count }
196+
</div>
197+
);
198+
},
199+
200+
onClick : function(){
201+
this.props.model.count = this.props.model.count + 1;
202+
}
203+
});
204+
```
205+
206+
Simplified NestedTypes-style type annotations can be used as props spec:
207+
- constructor functions: `Type`
208+
- constructors with default values: `Type.value( x )`
209+
- JSON and primitive values: `"default string"`
210+
211+
No other type annotation features are supported for `props`.
212+
213+
When component has `props` type spec:
214+
- React component propTypes will be automatically generated for every props;
215+
- if props has explicitly defined default value, getDefaultProps() method will be created. It means, that there are *no*
216+
default objects generated for simple `Type` style type spec.
217+
180218
## Pure Render Mixin
181219

182220
```javscript
183221
var MyComponent = React.createClass({
184-
propTypes : {
185-
item : PropTypes.instanceOf( MyModel ),
186-
elements : PropTypes.instanceOf( MyCollection ),
187-
className : PropTypes.string
222+
props : {
223+
item : MyModel,
224+
elements : MyCollection,
225+
className : String
188226
},
189227
190228
pureRender : true,
@@ -198,9 +236,9 @@ var MyComponent = React.createClass({
198236
```
199237

200238
PureRender optimization in enabled with `pureRender` option. It will create `shouldComponentUpdate` function
201-
which is optimized for props mentioned in `propTypes`.
239+
which is optimized for props mentioned in `propTypes` or `props` declaration.
202240

203-
Therefore, it's required to declare all of component props in `propTypes` when using this optimization.
241+
Therefore, it's required to declare all of component props when using this optimization.
204242

205243
## Data binding
206244

@@ -282,7 +320,7 @@ For link enclosings arrays and plain JS objects:
282320

283321
Link received through component props can be mapped as state member using the following declaration:
284322
```javascript
285-
attributes : {
323+
state : {
286324
selected : Nested.link( '^props.selectedLink' )
287325
}
288326
```
@@ -291,7 +329,7 @@ It can be accessed as a part of state, however, in this case it's not true state
291329
Also, links can be used to declaratively expose real component state to upper conponents. In this example, link optionally received in props will be updated every time `this.state.selected` object is replaced. In this case, updates are one way, from bottom component to upper one, and stateful component will render itself when state is changed.
292330

293331
```javascript
294-
attributes : {
332+
state : {
295333
selected : Item.has.watcher( '^props.selectedLink.val' )
296334
}
297335
```

nestedreact.js

Lines changed: 114 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nestedreact.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"nestedreact.js.map"
3333
],
3434
"license": "MIT",
35-
"version": "0.5.0-rc7",
35+
"version": "0.5.0-rc8",
3636
"scripts": {
3737
"build": "./node_modules/.bin/webpack",
3838
"watch": "./node_modules/.bin/webpack --watch"

0 commit comments

Comments
 (0)