question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get ref's style or width?

See original GitHub issue
<View ref='view' style={{width: 100, marginTop: 10}}>
  <View ref='inner' style={{flex: 1}} />
</View>

I want to get the offsetWidth of refs.view, thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

13reactions
gabrocommented, Apr 23, 2015

Ok, I managed to achieve my goal by measuring the view asynchronously (with a setTimeout)

  componentDidMount() {
    setTimeout(this.measureHeader);
  },

  measureHeader() {
    this.refs.header.measure((ox, oy, width, height) => {
      this.setState({headerHeight: height});
    });
  },

then I use this.state.headerHeight in my render method to computer another element’s position.

Still feels like a hack though…

11reactions
brentvatnecommented, Apr 22, 2015

@gabro @leecade - this is possible with the measure function: https://github.com/facebook/react-native/blob/master/Libraries/ReactIOS/NativeMethodsMixin.js#L61-L63

for example:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
} = React;


var TestIt = React.createClass({
  measureWelcome() {
    this.refs.welcome.measure(this.logWelcomeLayout);
  },

  logWelcomeLayout(ox, oy, width, height, px, py) {
    console.log("ox: " + ox);
    console.log("oy: " + oy);
    console.log("width: " + width);
    console.log("height: " + height);
    console.log("px: " + px);
    console.log("py: " + py);
  },

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome} ref="welcome">
          Welcome to React Native!
        </Text>
        <TouchableOpacity onPress={this.measureWelcome}>
          <Text>Measure it</Text>
        </TouchableOpacity>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('TestIt', () => TestIt);

The result of clicking the button:

2015-04-22 15:34:44.969 [info][tid:com.facebook.React.JavaScript] "ox: 72"
2015-04-22 15:34:44.969 [info][tid:com.facebook.React.JavaScript] "oy: 313"
2015-04-22 15:34:44.969 [info][tid:com.facebook.React.JavaScript] "width: 231.5"
2015-04-22 15:34:44.970 [info][tid:com.facebook.React.JavaScript] "height: 24"
2015-04-22 15:34:44.971 [info][tid:com.facebook.React.JavaScript] "px: 72"
2015-04-22 15:34:44.971 [info][tid:com.facebook.React.JavaScript] "py: 313"

measure uses the RCTUIManager measure function. Notice the return values to the callback:

    callback(@[
      @(frame.origin.x),
      @(frame.origin.y),
      @(frame.size.width),
      @(frame.size.height),
      @(pagePoint.x),
      @(pagePoint.y)
    ]);

You may also be interested in measureLayout, which gives you:

callback(@[@(leftOffset), @(topOffset), @(width), @(height)]);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get width of element using React ref? - Stack Overflow
Just wrap the Select component in a div and give that div the ref . Additionally, try to use state to record the...
Read more >
Using a Ref to change an Element's style in React | bobbyhadz
To use a ref to change an element's style in React, set the `ref` prop on the element. Get access to the element...
Read more >
vue ref width, height - CodePen
CSS ; 1 .box{ ; 2. width: 50%; ; 3. height: 200px; ; 4. background: pink; ; 5. }.
Read more >
Working with refs in React | CSS-Tricks
How a conversation between a child component and an element containing the ref might go down. This is a component that renders some...
Read more >
HTML DOM Style width Property - W3Schools
The width property has effect only on block-level elements or on elements with absolute or fixed position. The overflowing content can be manipulated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found