如何獲取reactjs中元素的高度以便在其他元素的樣式中使用它?如何獲取reactjs中元素的高度?
我的代碼有什麼問題?
import React, { Component } from 'react';
import TextField from 'material-ui/TextField';
class Messages extends Component {
constructor(props) {
super(props)
this.state = {
height: 0
}
}
componentDidMount() {
const height = this.input.clientHeight;
this.setState({ height: height });
}
render() {
const outputStyle = {
overflowY:'scroll',
height:`calc(100%-${this.state.height})`
};
const inputStyle = {
position:'fixed',
bottom:'0',
height:'auto'
};
return (
<div style={{height:'100%'}}>
<div name="output" style={outputStyle}/>
<TextField name="input" multiLine={true} fullWidth={true} underlineShow={false} style={inputStyle} rows={2} ref={(input) => {this.input = input;}} />
</div>
);
}
}
export default Messages;
我的目標是通過這種方式定義元素「輸出」的高度:100% - this.state.height。
在'setState'後面調用'this.state'仍然會給你初始值。嘗試渲染方法內的log.console。看看狀態的值 – abdul
嘗試'this.setState({height:height},()=> {console.log(this.state.height)});'看看這個日誌 – bennygenel
我得到了什麼「undefined 「在日誌 – juavenel