-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DIG-suggestion changed componentheight to class
- Loading branch information
Martin Havig
committed
May 4, 2016
1 parent
55847b3
commit 49c6ed5
Showing
2 changed files
with
25 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters