Creating a React Custom Element Wrapper Generator
Tags: wrapper, webcomponents, react
Published: 2019-06-20 11:16:51

---
title: Creating a React Custom Element Wrapper Generator
published: true
tags: wrapper,web-components,react
canonical_url: https://medium.com/@gilfink/creating-a-react-custom-element-wrapper-generator-68003d93eadf
---

![](https://cdn-images-1.medium.com/max/192/1*N-eo9u-zmpYdKqJITKle1Q.jpeg)<figcaption>Web Components</figcaption>

A few days ago I published a post called “[Wrapping React Components Inside Custom Element](https://dev.to/gilfink/wrapping-react-components-inside-custom-elements-3fph)”. 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](https://dev.to/gilfink/wrapping-react-components-inside-custom-elements-3fph), 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:

{% gist https://gist.github.com/gilf/d3a16fed27b33aa424109daacf2c29cf %}

#### Defining The Custom Element Wrapper Generator

At first, we will create the generator function definition, which will receive the React component class/function and the name of the custom element:

```
export const defineReactComponent = (Component, name) => {
   ...
}
```

After we created the generator function, in the function we will extract the observed attributes name from the component and then create the custom element class. At the end of the function, we will define the new custom element in the **CustomElementRegistry**. Here is the whole function implementation:

{% gist https://gist.github.com/gilf/2ce40e232356c9295afe3fffbaa85a96 %}

If you read my previous post most of this implementation should be familiar. A few things to notice:

- We set the observed attributes array at the beginning of the function.
- The **generateAllProps** function will be responsible to copy all the attributes to the props object which is passed to the React component. This solution is naive and isn’t covering things like data structures or functions. I’ll give you the opportunity to continue from here and build a more robust solution.

The end solution:

{% gist https://gist.github.com/gilf/ea9d6f72d23c3bbdf52143ad68cb8112 %}

#### Summary

In the post, I converted a specific solution I used in the “[Wrapping React Components Inside Custom Element](https://dev.to/gilfink/wrapping-react-components-inside-custom-elements-3fph)” post to a more generic implementation to wrap the React component inside a custom element. I hope that this starting point will help you to go forward and create a more robust solution.

Let me know what do you think in the comments.