DEV Community

Ernesto Jara Olveda
Ernesto Jara Olveda

Posted on

3 2

Create React Portals Typed Safe

today I will show you how to create a react portals with typescript the Propper way.

import ReactDOM from "react-dom";
import * as React from "react";

const defaultProps = {
  tag: "section",
  create:false,
  className: "",
};

type Props = React.HTMLAttributes<HTMLElement> & typeof defaultProps &{
  /**
   * the html tag you would like to use as wrapper.
   * 
   * @default "section"
   */
    tag: keyof JSX.IntrinsicElements;
    id: string;
    /**
     * if true a new node/htmlTag will be created, else the html tag already exists.
     * @default false.
     */
    create: boolean;
}

const Portal: React.FC<Props> = props => {
  const { tag, create, id, className, children, ...rest } = props;
    const el = create ? document.createElement(tag) : document.getElementById(id);

    const wrapper = React.useRef<HTMLElement>(el);


        React.useEffect(() => {
        const current = wrapper.current as HTMLElement;
        if (!current) return;

            current.setAttribute("id", id);
            current.setAttribute("class", className);

            type K =  keyof typeof rest;

            Object.keys(rest).forEach(key => {
              const val = rest[key as K] as K
                current.setAttribute(key, val);
            });

            document.body.appendChild(current);


        return () => {
            if (create) {
                document.body.removeChild(current);
            }
        };
    }, [wrapper,id, create, className, rest]);

      if (!wrapper.current) {
        return <>{null}</>;
    }

    return ReactDOM.createPortal(children, wrapper.current);
};

export default Portal;
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

👋 Kindness is contagious

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

Okay