diff --git a/package.json b/package.json index e07eeb7..485716f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/load-child.js b/src/load-child.js index f44a2cb..42190e7 100644 --- a/src/load-child.js +++ b/src/load-child.js @@ -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'; +}; diff --git a/tests/unit/load-child.js b/tests/unit/load-child.js index 14b2234..d7a0b72 100644 --- a/tests/unit/load-child.js +++ b/tests/unit/load-child.js @@ -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', @@ -25,7 +36,12 @@ describe('UNIT Load child', function() { component: SecondComponent, ref: 'fooChild' }; - }) + }), + omittedRef: function() { + return { + component: ThirdComponent + }; + } } }; @@ -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; + }); });