iDevie
November 2021
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories


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!

  • CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn’t been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we…

  • 7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should…

  • Create Classy Inputs Using MooTools’ OverText
  • Create a Simple Slideshow Using MooTools, Part IV: Thumbnails and Captions

Comments 0
There are currently no comments.