react-addons-text-content
Version:
Like DOM API's `Node.textContent` and it works in React Element
31 lines (25 loc) • 599 B
JavaScript
;
function getText(rootChild) {
var res = '';
var rr = function rr(child) {
if (typeof child === 'string' || typeof child === 'number') {
res += child;
} else if (Array.isArray(child)) {
child.forEach(function (c) {
return rr(c);
});
} else if (child && child.props) {
var children = child.props.children;
if (Array.isArray(children)) {
children.forEach(function (c) {
return rr(c);
});
} else {
rr(children);
}
}
};
rr(rootChild);
return res;
}
module.exports = getText;