Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

CarbonAds.js

45 lines1013 Bytes

1import React from 'react'
2​
3function buildScript(src, attrs = {}) {
4 if (typeof document !== 'undefined') {
5 const script = document.createElement('script')
6 script.async = true
7 script.defer = true
8 Object.keys(attrs).forEach(attr => script.setAttribute(attr, attrs[attr]))
9 script.src = src
10​
11 return script
12 }
14​
15export default function CarbonAds() {
16 const ref = React.useRef()
17​
18 React.useEffect(() => {
19 const script = buildScript(
20 '//cdn.carbonads.com/carbon.js?serve=CESI4KJY&placement=react-locationtanstackcom',
21 {
22 type: 'text/javascript',
23 id: '_carbonads_js',
24 },
25 )
26​
27 ref.current.appendChild(script)
28 }, [])
29​
30 React.useEffect(() => {
31 const interval = setInterval(() => {
32 ;[...ref.current.children].forEach(child => {
33 if (child.id.startsWith('carbonads_')) {
34 ref.current.removeChild(child)
35 }
36 })
37 }, 100)
38​
39 return () => {
40 clearInterval(interval)
41 }
42 })
43​
44 return <div ref={ref} />