DEV Community

Ernesto Jara Olveda
Ernesto Jara Olveda

Posted on

4 2

Create Custom HTML Components React

This generic component is useful if you are creating your own ui library and you want your components accept not only the props you want them to have, but also all the native props. for example you want your Button component to accept and auto complete the eventHandlers, aria-props and stuff w/out specifying'em.


import React from "react";

const defaultProps = {
  tag: "section",
};

type Props = React.HTMLAttributes<HTMLElement> & typeof defaultProps & {
    tag: React.ElementType;
    className: string;
    cssModule?: CSSModule;
}

const Html: React.FC<Props> = (props) => {
    const { tag: Tag, ...attributes } = props;

    return <Tag {...attributes} className={classes} />;
};

Section.defaultProps = defaultProps;

export default Html;
Enter fullscreen mode Exit fullscreen mode

now when you create your curstom button you'll see it will accept the props you set and also the "native props"

<Html tag="button" onClick={e=>{e.preventDefault();}}/>
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay