Created
August 27, 2017 06:18
-
-
Save blackmiaool/ea2ff3cfaf7ebb144fd81c4623c6c447 to your computer and use it in GitHub Desktop.
react children traverser
This file contains hidden or 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
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