Skip to content

Instantly share code, notes, and snippets.

@geraldfullam
Created December 7, 2018 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldfullam/83c54427f598aca49aee7301fb547672 to your computer and use it in GitHub Desktop.
Save geraldfullam/83c54427f598aca49aee7301fb547672 to your computer and use it in GitHub Desktop.
import React from 'react'
export function isComponentWithName (el, name) {
if (typeof name === 'string') {
return React.isValidElement(el) && name === getDisplayName(el)
}
const validRegEx = name instanceof RegExp ? name : new RegExp(name)
return React.isValidElement(el) && validRegEx.test(getDisplayName(el))
}
export function isComponentWithType (el, type) {
return React.isValidElement(el) && el.type === type
}
export function getDisplayName (el) {
if (React.isValidElement(el)) {
return el.type.displayName || el.type.name || 'Component'
}
return ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment