Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
590 changes: 590 additions & 0 deletions examples/src/components/app.js

Large diffs are not rendered by default.

71 changes: 35 additions & 36 deletions examples/src/components/container-box.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
import React, { Component } from 'react';
import React, {
useEffect, useRef, useState, useCallback,
} from 'react';
import { debounce } from 'throttle-debounce'


class ContainerBox extends Component {
constructor() {
super();
function ContainerBox({ isHeight }) {
const [width, setWidth] = useState('---');
const [height, setHeight] = useState('---');

this.box = React.createRef()
this.state = {
width: '---',
height: '---'
};
}
const box = useRef();

const setContainerWidthAndHeight = useCallback(() => {
setWidth(box.current.parentNode.offsetWidth);
setHeight(box.current.parentNode.offsetHeight);
}, [box]);

componentDidMount() {
this.setState({
width: this.box.current.parentNode.offsetWidth,
height: this.box.current.parentNode.offsetHeight
});
useEffect(() => {
setContainerWidthAndHeight();

window.addEventListener('resize', debounce(400, () => {
this.setState({
width: this.box.current.parentNode.offsetWidth,
height: this.box.current.parentNode.offsetHeight
});
})
);
}

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

return (
<div
ref={this.box}
className="container-width-box"
>
container {isHeight ? '' : 'width:'} <span>{width}</span> {isHeight ? `x ${height}` : ''} px
</div>
)
}
setContainerWidthAndHeight();
}));

return () => {
window.removeEventListener('resize', debounce);
};
},[]);

return(
<div ref={box} className="container-width-box">
container
{' '}
{isHeight ? '' : 'width:'}
{' '}
<span>{width}</span>
{' '}
{isHeight ? `x ${height}` : ''}
{' '}
px
</div>
);
}

export default ContainerBox;
Loading