Trying to append my react elements into a third-party rendered component(DOM element) without touching the third-party package.
SomeComponent defines and renders the tools data internally, it doesn't provide an API to custom or extend the tools. So I want to directly extend the tools view via DOM manipulation.
third-party.tsx:
import * as React from 'react';
export const SomeComponent = () => {
const tools = [
{ value: 1, action: () => console.log('a') },
{ value: 2, action: () => console.log('b') },
{ value: 3, action: () => console.log('c') },
];
return (
<div>
<ul className="tools-wrapper">
{tools.map((tool) => (
<li onClick={tool.action} key={tool.value}>
{tool.value}
</li>
))}
</ul>
</div>
);
};
App.tsx:
import * as React from 'react';
import './style.css';
import { SomeComponent } from './third-party';
export default function App() {
const customTools = [
{ value: 100, action: () => console.log('hello') },
{ value: 100, action: () => console.log('world') },
];
const customToolElements = (
<React.Fragment>
{customTools.map((tool) => (
<li key={tool.value} onClick={tool.action}>
{tool.value}
</li>
))}
</React.Fragment>
);
React.useEffect(() => {
const toolsWrapper = document.querySelector('.tools-wrapper');
// Append react elements into third-party rendered DOM element.
// Of course, it throws an error, customToolElements is not a DOM native Node type.
toolsWrapper.appendChild(customToolElements);
}, []);
return (
<div>
<SomeComponent />
</div>
);
}
Is this possible to extend the component of third-party via DOM manipulation directly rather than a data-driven API?