2016-12-05 50 views
-1

我使用如何在反应中查看div的样式属性?

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

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

打字稿返回错误

TS2531:对象可能是空。

我需要liqcont div的样式属性来查看哪些类属性已被实际渲染。任何其他方式来做到这一点?

+0

反应只是一个坏主意:D – ixpl0

+0

您是否尝试过参考? – Boky

+0

我还没有尝试与裁判。你能告诉我该怎么做吗? – newCoderNoCoder

回答

0

您可以尝试使用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')); 

希望这有助于。

+0

错误消息 - “'ReactInstance'@boky类型中不存在'TS2339:属性'属性'' – newCoderNoCoder