Hello Friends,
Welcome To Infinitbility!
The simplest way of scroll to position in react native using ScrollView ref.
Introduction
The simplest way of auto-scroll store scrolled position on our state and use in the vertical and horizontal auto-scroll.
but how it is possible auto-scroll in react native?
for auto-scroll, we are use scrollView as a ref, and ScrollView provides scrollTo
function for scrolling on any position in a scroll view.
First Create Refs on Class
Refs provide a way to access DOM nodes or React elements created in the render method. ~ from the react-native document
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.ScrollView = React.createRef();
}
}
Store scrolled height and width on state
State generally use in react-native to store data.
<ScrollView
showsVerticalScrollIndicator={false}
// define ref
ref={ref => (this.ScrollView = ref)}
// use for store scrolled value
onScroll={event =>
this.setState({horizontalScroll:event.nativeEvent.contentOffset.x, VerticalScroll:event.nativeEvent.contentOffset.y})
}
>
// Your list data
</ScrollView>
Use ScrollView ref to scroll to a destination position
// auto scroll function
autosScroll(){
// X use for horizontal
let horizontalScroll = this.state.horizontalScroll;
// Y use for Vertical
let VerticalScroll = this.state.VerticalScroll;
this.ScrollView.scrollTo({x: horizontalScroll, y: VerticalScroll, animated: true});
}
Thanks for reading…
More From React Native Tutorial
Basics
1. Introduction To React Native
2. React Native Environment Setup using expo
3. React Native Environment Setup for windows
4. React Native Environment setup on Mac OS
5. React Native Environment setup on linux
6. React Native Project Structure
Advances
4. React Native DatepickerAndroid
5. React native ScrollView scroll to position
6. How to align icon with text in react native
8. React Native Firebase Crashlytics
Error & Issue Solution
1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
2. Expiring Daemon because JVM heap space is exhausted In React Native
3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
5. App crashed immediately after install react native video or track player
6. how to delete SQLite database in android react native
7. React native material dropdown twice click issue
8. How to get the current route in react-navigation?
9. how to disable drawer on the drawer navigation screen?
10. Image not showing in ios 14 react native
11. React Native image picker launchimagelibrary on second time issue
12. how to open any link from react native render Html