I'm looking to append a button after the 2nd paragraph in a react html markdown implementation. I am currently using react-markdown to compile my html code. A few things I am trying to do here:
- Get nth element of the react markdown html (ex: p:2nd-child)
- Create new element (ex: document.createElement('button'))
- Append after the (p:2nd-child) element the new button
Should I use ref to achieve this or is it as simple as appending with plain JavaScript?? Open to other suggestions if there is a better solution.
index.jsx
import React, { useEffect } = 'react'
import ReactMarkdown from 'react-markdown'
const Markdown = (props) => {
// props.contentHTML seems to be typeof string (with no html tags.
//ex: 'hello world' vs '<p>hello world</p>') but converts to appropriate element tags
const reactHTML = <ReactMarkdown children={props.contentHTML} />
useEffect(() => {
// how do i get 2nd element of that reactHTML p tag??
let secondPElement = ?? reactHTML.querySelector..... ??
// create element for my button
let button = document.createElement('button');
button.id = 'btn-1';
// error here: appendChild does not exist on type 'Element'
reactHTML.appendChild(button)
})
return (
{reactHTML}
)
}
export default Markdown