Skip to content

A small function that allows you to inject React components onto dom nodes - transforming slots and attributes to props.

License

Notifications You must be signed in to change notification settings

RageLtd/dom-to-react

Repository files navigation

DOM-to-React

It does what it says on the tin.

Implementation

This library is basically a wrapper around react-dom's render function to do some lifting for you in relation to loading attributes in as props.

Usage

TL;DR - Function signature:

injectComponent(component: React.Component, element: Element, props = {}: Record<string, unknown>): void;

First include the package in your project:

npm i @nathandevuono/dom-to-react

Then use like this:

import injectComponent from '@nathandevuono/dom-to-react';

import MyComponent from './components/MyComponent';

function injectMyComponent() {
    const myElement = document.querySelector('#my-element');

    injectComponent(MyComponent, myElement);
}

window.addEventListener("DOMContentLoaded", injectMyComponent);

About

A small function that allows you to inject React components onto dom nodes - transforming slots and attributes to props.

Resources

License

Stars

Watchers

Forks