Enrikisimo Lopez Ramos
Enrikisimo Lopez Ramos

Reputation: 393

How to get width dynamically in react?

Im trying with measure but doesn´t work.

 this.refs.tabla.measure(this._getWidth);

_getWidth(a,f,width,h,x,z){
  width = width + '';
  this.setState({width});
  console.log(width);

}

this is the error: index.js:20222 Uncaught TypeError: this.refs.tabla.measure is not a function

Upvotes: 1

Views: 4518

Answers (1)

fabio.sussetto
fabio.sussetto

Reputation: 7055

If you have a ref to the element, you can do this.refs.myelem.clientWidth (which is zero for elements with no CSS or inline layout boxes) or maybe better this.refs.myelem.getBoundingClientRect().width Not sure where that measure method should be coming from.

It can be helpful to remember that React provides a standardised DOM implementation, which means you can normally work with React events and DOM element as if you were dealing with "standard" Js DOM node and events. Both getBoundingClientRect and clientWidth are available on standard DOM nodes too, nothing magic related to React.

Upvotes: 2

Related Questions