Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-component-tree",
"version": "0.3.0",
"version": "0.3.1",
"description": "Serialize and reproduce the state of an entire tree of React components",
"main": "dist/entry.js",
"repository": {
Expand Down
10 changes: 9 additions & 1 deletion src/load-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ var getChildParams = function(component, childName, a, b, c, d, e, f) {

// Default the child ref to its key name if the child template doesn't return
// a value
if (!params.ref) {
if (!params.ref && isClassComponent(params.component)) {
params.ref = childName;
}

return params;
};

var isClassComponent = function(Component) {
// Inspired from Recompose: http://bit.ly/1NCac7D
return Component &&
Component.prototype &&
// Only evidence this is reliable: http://bit.ly/1MQPRyU
typeof(Component.prototype.isReactComponent) === 'object';
};
29 changes: 26 additions & 3 deletions tests/unit/load-child.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
var React = require('react'),
_ = require('lodash'),
loadChild = require('../../src/load-child.js').loadChild;

var ReactComponent = {
prototype: {
isReactComponent: {}
}
};
var StatelessComponent = {
prototype: {}
};

describe('UNIT Load child', function() {
var FirstComponent = {},
SecondComponent = {},
var FirstComponent = _.cloneDeep(ReactComponent),
SecondComponent =_.cloneDeep(ReactComponent),
ThirdComponent =_.cloneDeep(StatelessComponent),
component,
children = [React.createElement('span', {
key: '1',
Expand All @@ -25,7 +36,12 @@ describe('UNIT Load child', function() {
component: SecondComponent,
ref: 'fooChild'
};
})
}),
omittedRef: function() {
return {
component: ThirdComponent
};
}
}
};

Expand Down Expand Up @@ -101,4 +117,11 @@ describe('UNIT Load child', function() {
var props = React.createElement.lastCall.args[1];
expect(props.ref).to.equal('fooChild');
});

it('should omit ref for stateless components', function() {
loadChild(component, 'omittedRef');

var props = React.createElement.lastCall.args[1];
expect(props.ref).to.be.undefined;
});
});