【问题标题】:How can I view the style attributes of a div in react?如何在反应中查看 div 的样式属性?
【发布时间】:2017-04-19 13:33:47
【问题描述】:

我正在使用

console.log("+++++++++++++++++++++++++", document.getElementById("liqcont").attributes);

在 .tsx 文件的 return{} 部分之前。

Typescript 返回错误

TS2531:对象可能为空。

我需要 liqcont div 的样式属性来查看实际呈现了哪些类属性。还有其他方法吗?

【问题讨论】:

  • 反应只是个坏主意:D
  • 你用 refs 试过了吗?
  • 我没有尝试过使用 refs。你能告诉我怎么做吗?

标签: css reactjs typescript


【解决方案1】:

您可以尝试使用 refs,然后在 componentDidMount 中使用 ref 获取该元素的所有属性。像这样的东西应该可以工作:

class Test extends React.Component {
    componentDidMount(){
     let attributes = this.refs["wrapper"].attributes;
    }


    render(){
        return (
           <div ref="wrapper">
             ........
           </div>
      )
    }
}

React.render(<Test />, document.getElementById('container'));

希望这会有所帮助。

【讨论】:

  • 错误消息 - 'TS2339:属性'attributes' 不存在于类型'ReactInstance' @boky
猜你喜欢
  • 2011-05-29
  • 2019-10-31
  • 2020-05-09
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
  • 2019-02-19
相关资源
最近更新 更多