I created this little function to find children of a specific type.
I'd like to be able to select children via property like jQuery, [name="example"]. Is there any way to strip the string selector functionality from jQuery to create an object map to feed to something like lodash's _.find?
I realize this is a fake solution for selecting / finding / filtering an array but I think it can be really useful. I also know that it won't find deep children, it would just deal with direct root children.
Does this exist within react already?
function containsElement (element, children) {
var result = []
element = _.flatten([element])
if (React.isValidElement(children) && !_.isArray(children)) {
var contains = _.contains(element, children.type)
if (contains) return children
return false
} else if (!React.isValidElement(children) && !_.isArray(children)) {
return false
}
children.forEach(function (child) {
var isElm = React.isValidElement(child)
var contains = _.contains(element, child.type)
if (isElm && contains) {
result.push(child)
}
})
if (!result.length) return false
return result
}
document.querySelectorAllwhich uses the css selector engine. You could also instead use sizzle, which is what jQuery's selector engine is built upon.this.props.children.Sizzle.matches('tr', this.props.children)didn't work. Returning empty array. Could it be because it's not a real DOM element array?