Hello All,
I am beginner in reactjs and i want to embed google inline ads in loops. it is only showing at first time. but in inspect element tag shows in loop.
Google adsense code:-
var ScheduleRow = React.createClass({
var rows = _.map(scheduleData.schedules, function(scheduleList, i) {
var divStyle = { display: "block"};
return (
<ins className="adsbygoogle"
style={divStyle}
data-ad-client="ca-pub-3199660652950290"
data-ad-slot="6259591966"
data-ad-format="auto" key={i}>
</ins>
);
});
return (
<span>
{rows}
</span>
);
});
Output:-
Inspect Element Output:-
Thanks,
Vikram
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.
This seems a duplicated question. You can find it in here. But I think it isn't 100% clear. So, I came across once with this blog post which is more clear.
From Google you have this:
Now, in your react app:
Include the following snippet in your index.html
Create your react component like:
Now, to render it multiple times you can simply wrap the
ins
html tag with an iterator likemap
. However, I don't fully understand your need here.If you want to show them all at once, then do your map like this.
If you want to randomise your ad, add a state to your component and a tick state so that it can re-render every X seconds. Check it in this SO answer
Notes:
class
attribute toclassName
style
attribute to be wrapped like this:style={{ display: 'block' }}