Skip to content

Instantly share code, notes, and snippets.

@blehr
Last active June 22, 2016 19:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blehr/6fd24b76554e445d95029ca9430603c2 to your computer and use it in GitHub Desktop.
Save blehr/6fd24b76554e445d95029ca9430603c2 to your computer and use it in GitHub Desktop.
GoogleAd React Component
import React, { Component, PropTypes } from 'react';
export default class GoogleAd extends Component {
static propTypes = {
client: PropTypes.string,
slot: PropTypes.string,
format: PropTypes.string,
wrapperDivStyle: PropTypes.object
}
constructor(props) {
super(props);
}
// This code is ran when the component mounts
componentDidMount() {
(window.adsbygoogle = window.adsbygoogle || []).push({});
}
// an outer div for styling purposes
// changed class to ClassName
// changed style from string to an object
render() {
return (
<div style={this.props.wrapperDivStyle} >
<ins className="adsbygoogle"
style={{'display': 'block'}}
data-ad-client={this.props.client}
data-ad-slot={this.props.slot}
data-ad-format={this.props.format}>
</ins>
</div>
);
}
}
************************************************************
// To use the Component
import React, { Component, PropTypes } from 'react';
import GoogleAd from './google_ad';
// create a style object that is applied
// to the div wrapping the adSense code
// no styling required - just leave style object empty
const style = {
marginTop: '15px',
marginBottom: '20px'
};
const SomeComponent = props => {
return (
<GoogleAd
client="ca-pub-xxxxxxxxxx"
slot="xxxxxxxxxx"
format="auto"
wrapperDivStyle={style}
/>
);
};
export default SomeComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment