Drawbacks of Simple Implementation
Explore the limitations of a basic polymorphic component implementation in React using TypeScript. Understand problems like passing invalid HTML elements and incorrect attributes, which can lead to rendering errors and poor development feedback. This lesson prepares you to recognize key flaws that need addressing for robust component design.
We'll cover the following...
We now have something of a standard polymorphic implementation. However, our implementation has a few problems. One of those problems is that the polymorphic prop can receive invalid HTML elements.
For example, it's currently possible for a user to go ahead and write something like the following:
<PolymorphicText as="educative">Hello Wrong Element</PolymorphicText>
The as prop passed here is educative.
The educative prop is obviously an incorrect HTML element, but the browser still tries to render it.
Inspecting the rendered element
Click the "Run" button and inspect the element rendered in the playground widget below.