How to Access styles from React?

前端 未结 5 1637
时光取名叫无心
时光取名叫无心 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:49

    It's worth noting that while ReactDOM.findDOMNode is usable today, it will be deprecated in the future in place of callback refs.

    There is a post here by Dan Abramov which outlines reasons for not using findDOMNode while providing examples of how to replace the use of ReactDOM.findDOMNode with callback refs.

    Since I've seen SO users get upset when only a link was included in an answer, so I will pass along one of the examples Dan has kindly provided:

    findDOMNode(stringDOMRef)

    **Before:**
    
    class MyComponent extends Component {
      componentDidMount() {
        findDOMNode(this.refs.something).scrollIntoView();
      }
    
      render() {
        return (
          
    ) } } **After:** class MyComponent extends Component { componentDidMount() { this.something.scrollIntoView(); } render() { return (
    this.something = node} />
    ) } }

提交回复
热议问题