ReactJS 获取渲染组件高度

标签 reactjs

我正在尝试集成或创建 https://github.com/kumailht/gridforms 的 React 版本,为此,我需要标准化行内列的高度。原始数据采用网格行的高度并将其应用于子列。

我计划获取行的高度,然后将其映射到子项的属性,尽管从我的尝试来看,我认为这可能不是理想的方式,甚至可能不是?

下面是我当前的代码。

GridRow = React.createClass({
  render(){
        const children = _.map(this.props.children, child => {
            child.props.height = // somehow get row component height
            return child
        })
    return (<div data-row-span={this.props.span} {...this.props}>
      {children}
    </div>)
  }
})

GridCol = React.createClass({
  render(){
    return (<div data-field-span={this.props.span} style={{height:this.props.height}} {...this.props}>
      {this.props.children}
    </div>)
  }
})

我测试了以这种方式设置样式,它会起作用,但是获取高度却不行。

编辑: fiddle :

https://jsfiddle.net/4wm5bffn/2/

最佳答案

答案有点晚了,但从技术上讲,你可以通过这种方式获得元素高度:

var node = ReactDOM.findDOMNode(this.refs[ref-name]);
if (node){
  var calculatedHeight = node.clientHeight;
}

关于ReactJS 获取渲染组件高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31661790/

相关文章:

javascript - 警告 : findDOMNode is deprecated in StrictMode. findDOMNode 传递了一个位于 StrictMode 内的 Transition 实例

reactjs - 如何使用 Reactstrap 检查复选框的选中值

javascript - 上下文映射函数中的 jsx-no-bind

reactjs - 如何通过 react.js 发出 POST 请求?

javascript - 导出组件时文档不显示任何内容

node.js - npm test -- --coverage 永不退出

javascript - 如果需要如何加载数据? react /减少。无SSR

css - 使用 React 制作 RubberBand 动画

javascript - 类型错误 : Cannot read property 'current' of undefined focus

reactjs - 如何使用 react-test-library 测试复选框