How to Access styles from React?

前端 未结 5 1611
时光取名叫无心
时光取名叫无心 2020-12-24 14:10

I am trying to access the width and height styles of a div in React but I have been running into one problem. This is what I got so far:

componentDidMount()         


        
5条回答
  •  有刺的猬
    2020-12-24 14:50

    You should use ReactDOM.findDOMNode method and work from there. Here's the code that does what you need.

    var Hello = React.createClass({
    
    componentDidMount: function() {
      var elem = ReactDOM.findDOMNode(this.refs.container);
      console.log(elem.offsetWidth, elem.offsetHeight);    
    },
    
    render: function() {
       return (
          
    Hello world
    ); } }); ReactDOM.render( , document.getElementById('container') );

    jsFiddle

提交回复
热议问题