import jquery from 'jquery';

//动态获取窗口的宽度和高度
export default class Resize extends React.Component {
	constructor(props) {
		super(props);

		this.state = {
			width: 0,
			height: 0,

		}
	}

	set = () => {
		const width = jquery(window).width();
		const height = jquery(window).height();

		this.setState({
			width,
			height,
		});
	}

	componentDidMount() {
		this.set();

		jquery(window).bind('resize', () => {
			this.set();
		});
	}

	componentWillUnmount() {
		jquery(window).unbind('resize');
	}

	render() {
		const {
			width,
			height,
		} = this.state;

		return (
			<div>
				{this.props.children && React.cloneElement(
				  this.props.children,
				  {
				  	width,
					height,
				  }
				)}
			</div>
		);
	}
}