Skip to content

Instantly share code, notes, and snippets.

@zbyte64
Created May 30, 2014 18:17
Show Gist options
  • Save zbyte64/d66e091e3bc15a5c88d3 to your computer and use it in GitHub Desktop.
Save zbyte64/d66e091e3bc15a5c88d3 to your computer and use it in GitHub Desktop.
Deep cloning of react components
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);
}
@porkopek
Copy link

There is now _.cloneDeep https://lodash.com/docs/4.17.4#cloneDeep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment