@Speckledcarp और @ जेम्स द्वारा जवाब दोनों शानदार हैं। मेरे मामले में, हालाँकि, मुझे एक ऐसे घटक की आवश्यकता थी, जिसकी ऊँचाई पूरी खिड़की की ऊँचाई को बढ़ा सके, रेंडर समय पर सशर्त .... लेकिन भीतर एक एचओसी को कॉल render()
करने से पूरे उप-पुन: प्रतिपादन होता है । Baaad।
साथ ही, मुझे मानों को सहारा के रूप में प्राप्त करने में कोई दिलचस्पी नहीं थी, लेकिन बस एक माता-पिता चाहते थे div
जो पूरे स्क्रीन की ऊंचाई (या चौड़ाई, या दोनों) पर कब्जा कर लेंगे।
इसलिए मैंने एक पेरेंट कंपोनेंट लिखा जो एक पूर्ण ऊंचाई (और / या चौड़ाई) div प्रदान करता है। बूम।
एक उपयोग मामला:
class MyPage extends React.Component {
render() {
const { data, ...rest } = this.props
return data ? (
// My app uses templates which misbehave badly if you manually mess around with the container height, so leave the height alone here.
<div>Yay! render a page with some data. </div>
) : (
<FullArea vertical>
// You're now in a full height div, so containers will vertically justify properly
<GridContainer justify="center" alignItems="center" style={{ height: "inherit" }}>
<GridItem xs={12} sm={6}>
Page loading!
</GridItem>
</GridContainer>
</FullArea>
)
यहाँ घटक है:
import React, { Component } from 'react'
import PropTypes from 'prop-types'
class FullArea extends Component {
constructor(props) {
super(props)
this.state = {
width: 0,
height: 0,
}
this.getStyles = this.getStyles.bind(this)
this.updateWindowDimensions = this.updateWindowDimensions.bind(this)
}
componentDidMount() {
this.updateWindowDimensions()
window.addEventListener('resize', this.updateWindowDimensions)
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateWindowDimensions)
}
getStyles(vertical, horizontal) {
const styles = {}
if (vertical) {
styles.height = `${this.state.height}px`
}
if (horizontal) {
styles.width = `${this.state.width}px`
}
return styles
}
updateWindowDimensions() {
this.setState({ width: window.innerWidth, height: window.innerHeight })
}
render() {
const { vertical, horizontal } = this.props
return (
<div style={this.getStyles(vertical, horizontal)} >
{this.props.children}
</div>
)
}
}
FullArea.defaultProps = {
horizontal: false,
vertical: false,
}
FullArea.propTypes = {
horizontal: PropTypes.bool,
vertical: PropTypes.bool,
}
export default FullArea
.bind(this)
कॉलबैक तर्कों से हटा सकते हैं क्योंकि यह पहले से ही कंस्ट्रक्टर द्वारा बाध्य है।