React.isValidElement
Knowing what input type you've received is hugely important in JavaScript, which is a big reason for Flow and TypeScript's rise. One such case where it's useful to know what an object represents is if the input is a string or a React element.
To detect if an object is a React element, you can use React.isValidElement(obj)
:
// Add a wrapping DIV if the content isn't a React element
// PropTypes.oneOfType([PropTypes.string, PropTypes.element])
render() {
const { content } = this.props
React.isValidElement(content)) ?
content :
{content}
}
I really like that React.isValidElement
allows us to create flexible elements that accept React elements or strings; hugely useful in generic components like modals, alerts, and everywhere else!
I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event...
The first thing that hits you when you visit Pinterest is "Whoa, the columns are the same width and the photos are cut to fit just the way they should." Basic web users probably think nothing of it but as a developer, I can appreciate the...
I love progress bars. It's important that I know roughly what percentage of a task is complete. I've created a highly customizable MooTools progress bar class that animates to the desired percentage.
The Moo-Generated XHTML
This DIV structure is extremely simple and can be controlled...