-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
43 lines (33 loc) · 991 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React from 'react'
import ReactDOM from 'react-dom'
const TestComponent = () => (
<h1>I am dynamically added!</h1>
)
window.addEventListener('load', function() {
const watch = document.getElementById('target-test')
const observer = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
const target = watch.querySelector('p')
if (target) {
app(observer, target)
}
}
}
})
observer.observe(watch, {
attributes: true,
childList: true,
subtree: true
})
})
const app = (observer, target) => {
observer.disconnect()
if (!document.getElementById('react-root-test')) {
const parent = target.parentNode
const root = document.createElement('div')
root.setAttribute('id', 'react-root-test')
parent.insertBefore(root, target)
ReactDOM.render(<TestComponent />, document.getElementById('react-root-test'))
}
}