Created
November 10, 2015 15:37
-
-
Save slavikshen/7b29b06215b9e7a08455 to your computer and use it in GitHub Desktop.
How to get inner text in React virtual DOM
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
$(function() { | |
React.getInnerText = function(obj) { | |
var buf = ''; | |
if( obj ) { | |
var type = typeof(obj); | |
if( type === 'string' || type === 'number' ) { | |
buf += obj; | |
} else if( type === 'object' ) { | |
var children = null; | |
if( Array.isArray(obj) ) { | |
children = obj; | |
} else { | |
var props = obj.props; | |
if( props ) { | |
children = props.children; | |
} | |
} | |
if( children ) { | |
if( Array.isArray(children) ) { | |
children.forEach(function(o) { | |
buf += React.getInnerText(o); | |
}); | |
} else { | |
buf += React.getInnerText(children); | |
} | |
} | |
} | |
} | |
return buf; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment