Created
May 30, 2014 18:17
-
-
Save zbyte64/d66e091e3bc15a5c88d3 to your computer and use it in GitHub Desktop.
Deep cloning of react components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
function isComponent(obj) { | |
//ghetto check because JS | |
return (obj && obj.setState) | |
} | |
function deepCloneComponent(tpl) { | |
if (!tpl) return; | |
if (_.isArray(tpl)) { | |
return _.map(tpl, function(component) { | |
return deepCloneComponent(component); | |
}) | |
} | |
if (!isComponent(tpl)) return _.clone(tpl) | |
var new_props = _.clone(tpl.props); | |
if (new_props) new_props.children = deepCloneComponent(new_props.children); | |
return tpl.constructor.ConvenienceConstructor(new_props); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is now _.cloneDeep https://lodash.com/docs/4.17.4#cloneDeep