Skip to content

Commit

Permalink
DIG-suggestion changed componentheight to class
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Havig committed May 4, 2016
1 parent 55847b3 commit 49c6ed5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
51 changes: 23 additions & 28 deletions src/ffe-component-height.jsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
import React from 'react';
import { shouldComponentUpdate } from 'react/lib/ReactComponentWithPureRenderMixin';

const ReactHeight = React.createClass({
propTypes: {
children: React.PropTypes.node.isRequired,
onHeightReady: React.PropTypes.func.isRequired
},

getInitialState() {
return {
class ReactHeight extends React.Component {
constructor(props) {
super(props);
this.state = {
height: 0
};
},
this.componentDidMount = this.componentDidMount.bind(this);
this.shouldComponentUpdate = this.shouldComponentUpdate.bind(this);
this.setWrapperRef = this.setWrapperRef.bind(this);
}

componentDidMount() {
const height = this.wrapper.clientHeight;

this.setState({ height }, () => this.props.onHeightReady(this.state.height));
},

shouldComponentUpdate,

componentDidUpdate() {
if (this.wrapper) {
const height = this.wrapper.clientHeight;
if (height !== this.state.height) {
this.setState({ height }, () => this.props.onHeightReady(this.state.height)); //eslint-disable-line react/no-did-update-set-state
}
if (height !== this.state.height) {
this.setState({ height }, () => this.props.onHeightReady(this.state.height));
}
},
}

shouldComponentUpdate(nextProps, nextState) {
return nextState.height === this.state.height;
}

setWrapperRef(el) {
this.wrapper = el;
},
}

render() {
const { children, ...props } = this.props;

return <div ref={ this.setWrapperRef } {...props}>
const { children } = this.props;
return <div ref={ this.setWrapperRef }>
{ children }
</div>;
}
});
}

ReactHeight.propTypes = {
children: React.PropTypes.node.isRequired,
onHeightReady: React.PropTypes.func.isRequired
};

export default ReactHeight;
4 changes: 2 additions & 2 deletions src/ffe-expandable-react.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import ComponentHeight from './ffe-component-height';

class Expandable extends React.Component {

constructor() {
super();
constructor(props) {
super(props);
this.state = { height: -1 };
}

Expand Down

0 comments on commit 49c6ed5

Please sign in to comment.