How to embed Google Ad Sense into React Component?

后端 未结 3 1191
栀梦
栀梦 2020-12-09 00:13

The question at hand deals with Google Ad Sense specifically but could apply to any script tag insert. I don\'t understand how I could add something like this to my compon

3条回答
  •  臣服心动
    2020-12-09 00:51

    A third party script should not be necessary for something like this.

    Ad-Sense requires a third party script that looks like this, this should be loaded prior to react and or just put into a template if you're using any sort of template (i.e... django template etc...)

    put this in template:

    
    

    then take your ad sense include, remove the comment (assuming you're using JSX) and turn what google gave you

    what google gave you:

     
    
    
    
    
    

    change it to this:

    
    
    

    then put the actual execution code in the componentDidMount function

    (adsbygoogle = window.adsbygoogle || []).push({});
    

    I would imagine a similar solution would work for just about any of the same circumstances, not just Ad-Sense.

    One common misconception is that people often think they can put a script tag in a dangerouslySetInnerHTML attribute and that is not the case. It's using setInnerHTML which will not execute script tags.

提交回复
热议问题