How to embed Google Ad Sense into React Component?

后端 未结 3 1192
栀梦
栀梦 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

    Based on the awesome answer by Chris Hawkes, here is the solution adapted for Typescript.

    After you follow the steps above, you will get errors in componentDidMount() because the compiler does not know about the adsbygoogle variable and the adsbygoogle property on the Window object.

    To fix these issues:

    1. declare var adsbygoogle: any; globally (I did it in index.tsx, above all components)
    2. componentDidMount(), write(adsbygoogle = (window as any).adsbygoogle || []).push({});`

提交回复
热议问题