Skip to content

Instantly share code, notes, and snippets.

@blackmiaool
Created August 27, 2017 06:18
Show Gist options
  • Save blackmiaool/ea2ff3cfaf7ebb144fd81c4623c6c447 to your computer and use it in GitHub Desktop.
Save blackmiaool/ea2ff3cfaf7ebb144fd81c4623c6c447 to your computer and use it in GitHub Desktop.
react children traverser
function traverse(node, hierarchy) {
const className = node.props && node.props.style;//array
if (className) {
className.forEach((name, i) => {
if (typeof name === 'string') {
const styleObject = {};
if (style[name]) {
style[name].forEach((obj) => {
Object.assign(styleObject, obj.style);
});
}
className[i] = styleObject;
}
});
hierarchy.push(className);
}
if (node.props && node.props.children) {
if (Array.isArray(node.props.children)) {
node.props.children.forEach(child => traverse(child, hierarchy));
} else {
traverse(node.props.children, hierarchy);
}
}
hierarchy.pop();
}
// traverse(ret, []);
React.createElement = origianlCreateElement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment