简体   繁体   English

反应本地动态引用

[英]react native dynamic ref

I am trying to get some measurement from specefic views to see where the view is located. 我正在尝试从特定视图中获取一些度量值,以查看该视图位于何处。 After that I wanna implement a scrollto function. 之后,我想实现scrollto函数。

I am using this code for the handle: 我正在使用此代码作为句柄:

_handleItemClicked = (index) => () => {
  this.sectionItem[index].measure( (fx, fy, width, height, px, py) => {
    console.log('Component width is: ' + width)
    console.log('Component height is: ' + height)
    console.log('X offset to frame: ' + fx)
    console.log('Y offset to frame: ' + fy)
    console.log('X offset to page: ' + px)
    console.log('Y offset to page: ' + py)
  })
}

And I am using this code for the view 我正在使用此代码进行查看

renderItem = ({item, index, section}) => (
            <TouchableNativeFeedback ref={view => { this.sectionItem[index] = view; }} onPress={this._handleItemClicked(index)}>
               <View style={styles.itemrow} >
                 <View style={{width:80,marginLeft:10,height:30}}><Text style={{textAlign:'right',color:'#000',opacity:0.9,fontSize:18,marginTop:4}}>{item.title}</Text></View>
                 </View>
               </View>
             </TouchableNativeFeedback>
);

I tried some other methods but so far no luck. 我尝试了其他方法,但到目前为止还没有运气。 I'am always ending with some error. 我总是以一些错误结尾。 This time its undefined wich is not a object. 这次,它的未定义夹点不是对象。

looking forward to the answers. 期待答案。

Try this instead: 尝试以下方法:

ref = { view => ( this[`sectionItem${index}`] = view ) }

I am guessing its trying to find sectionItem in scope, but unable to, giving you the undefined error. 我猜想它试图在范围内找到sectionItem,但无法找到未定义的错误。

If you do it like the above, it puts a ref with an index in this. 如果您像上面那样操作,则会在其中放置带有索引的ref。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM