Creating a React Custom Element Wrapper Generator

A few days ago I published a post called “Wrapping React Components Inside Custom Element.” The post explained how to wrap a React component inside a custom element. At the end of the post, I wrote that I might think of a few ways to do the same functionality but automatic and more generic. In this post, I’ll explain one of those ideas.

Note: If you didn’t read the previous post, I suggest to read it to get some context for this post.

Observing Props

At first, I’ll take an assumption which is that the majority of React developers use the prop-types library to declare React prop types on their React component. Using this assumption, we can use the prop types to understand the names and the types of all the props that a component is receiving and to generate the observed attributes array for the custom element. We will create a getObserved helper function, which will be responsible to extract the names of the observed attributes from the prop types: