最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to scroll into View in React Native? - Stack Overflow

programmeradmin5浏览0评论

I'm developing an app using React Native. There, I want to scroll into a View when I press a button. I've tried something like this.

render() {
  return(
    <View ref={field => this.myView = field} >
      //something inside the view
    </View>
  )
}

then, tried

this.myView.focus()

But, it doesn't work.

I want to get the result as shown in the following GIF demo. How to do this?

I'm developing an app using React Native. There, I want to scroll into a View when I press a button. I've tried something like this.

render() {
  return(
    <View ref={field => this.myView = field} >
      //something inside the view
    </View>
  )
}

then, tried

this.myView.focus()

But, it doesn't work.

I want to get the result as shown in the following GIF demo. How to do this?

Share Improve this question asked Sep 14, 2019 at 8:10 Sennen RandikaSennen Randika 1,6363 gold badges17 silver badges35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

You should store ScrollView ref and use scrollViewRef.scrollTo()

render() {
  return(
    <ScrollView ref={ref => this.scrollViewRef = ref}>
      <View 
        // you can store layout for each of the fields
        onLayout={event => this.layout = event.nativeEvent.layout}
      > 
        // some field goes here
      </View>
    </ScrollView>
  )
}

And then when an error occurs just scroll to your field this.scrollViewRef.scrollTo({ y: this.layout.y, animated: true });.

Update: a working example can be found in this repo.

发布评论

评论列表(0)

  1. 暂无评论