在react中插入textarea,由后端返回的内容决定textarea的高度。
import React from 'react';
import {Input} from 'antd';
import $ from 'jquery';
const { TextArea } = Input;
let valueShow='文本文本文本文本文本文本文本文本文本文本文本文本文本文本'
export default class Index extends Component {
componentDidMount() {
this.adaptiveHeight()
}
componentDidUpdate() {
this.adaptiveHeight()
}
adaptiveHeight = () => {
$('textarea').height(0)//先初始化一下,避免记录上一下的高度
$('textarea').each(function () {
//this指向textarea本身
this.setAttribute('style', 'height:' + (this.scrollHeight) + 'px;overflow-y:hidden;');
})
}
render() {
return (
<div>
//readOnly 只读,不可编辑
<TextArea readOnly value={valueShow} />
</div>
)
}
}