How to embed google adsense in react js

前端 未结 3 1039
青春惊慌失措
青春惊慌失措 2020-12-13 01:20

I am beginner in reactjs and I want to embed google inline ads in loops. The ad is showing only at first time. When I inspect the element tag shows in loop. Can I please kno

3条回答
  •  感动是毒
    2020-12-13 01:30

    Answer by @jpgbarbosa is great. I'll add better practice for Server Side Rendered React applications which have multiple pages, for scalability, I suggest you use this method to keep code base maintainable.

    export default class HomePage extends React.Component {
      componentDidMount() {
        const installGoogleAds = () => {
          const elem = document.createElement("script");
          elem.src =
            "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
          elem.async = true;
          elem.defer = true;
          document.body.insertBefore(elem, document.body.firstChild);
        };
        installGoogleAds();
        (adsbygoogle = window.adsbygoogle || []).push({});
      }
    
      render() {
        return (
          
        );
      }
    }
    

提交回复
热议问题