Skip to main content

React Native ScrollView

The ScrollView is a generic scrollable container, which scrolls multiple child components and views inside it. In the ScrollView, we can scroll the components in both direction vertically and horizontally. By default, the ScrollView container scrolls its components and views in vertical. To scroll its components in horizontal, it uses the props horizontal: true (default, horizontal: false).

Props of ScrollView

alwaysBounceVerticalonScrollhorizontal
contentContainerStylescrollEnabledbouncesZoomzoomScale
onScrollBeginDragonContentSizeChangemaximumZoomScaleminimumZoomScale
onScrollBeginDragonContentSizeChangemaximumZoomScaleminimumZoomScale
onScrollEndDragcenterContentcontentInsetrefreshControl
pagingEnabledscrollsToTopsnapToAlignmentshowsHorizontalScrollIndicator
snapToStartsnapToEndindicatorStyleshowsHorizontalScrollIndicator

React Native ScrollView Example

In this example, we create a vertical ScrollView using Text and Button components.
App.js

  1. import React, { Component } from 'react';  
  2. import { AppRegistry, ScrollView, Image, Text, Button, StyleSheet } from 'react-native';  
  3.   
  4. export default class ScrolledViewExample extends Component {  
  5.     onPressButton() {  
  6.         alert('You clicked the button!')  
  7.     }  
  8.   
  9.     render() {  
  10.         return (  
  11.             <ScrollView >  
  12.                 <Text style={{fontSize:20}}>Scroll me plz</Text>  
  13.                 <Button title={'Button 1'} onPress={this.onPressButton} />  
  14.                 <Text style={{fontSize:20}}>React Native Example of ScrollView</Text>  
  15.                 <Button title={'Button 2'} onPress={this.onPressButton}/>  
  16.                 <Text style={{fontSize:20}}>React Native ScrollView Example</Text>  
  17.                 <Button title={'Button 3'} onPress={this.onPressButton}/>  
  18.                 <Text style={{fontSize:20}}>If you like</Text>  
  19.                 <Button title={'Button 4'} onPress={this.onPressButton}/>  
  20.                 <Text style={{fontSize:20}}>Scrolling down</Text>  
  21.                 <Button title={'Button 5'} onPress={this.onPressButton}/>  
  22.                 <Text style={{fontSize:20}}>Scrolling down</Text>  
  23.                 <Button title={'Button 6'} onPress={this.onPressButton}/>  
  24.                 <Text style={{fontSize:20}}>What's the best</Text>  
  25.                 <Button title={'Button 7'} onPress={this.onPressButton}/>  
  26.                 <Text style={{fontSize:20}}>What's the best</Text>  
  27.                 <Button title={'Button 8'} onPress={this.onPressButton}/>  
  28.                 <Text style={{fontSize:20}}>Framework around?</Text>  
  29.                 <Button title={'Button 9'} onPress={this.onPressButton}/>  
  30.                 <Text style={{fontSize:20}}>Framework around?</Text>  
  31.                 <Button title={'Button 10'} onPress={this.onPressButton}/>  
  32.                 <Text style={{fontSize:20}}>React Native</Text>  
  33.                 <Button title={'Button 11'} onPress={this.onPressButton}/>  
  34.                 <Text style={{fontSize:20}}>Scroll me plz</Text>  
  35.                 <Button title={'Button 12'} onPress={this.onPressButton} />  
  36.                 <Text style={{fontSize:20}}>Scroll me plz</Text>  
  37.                 <Button title={'Button 13'} onPress={this.onPressButton}/>  
  38.                 <Text style={{fontSize:20}}>If you like</Text>  
  39.                 <Button title={'Button 14'} onPress={this.onPressButton}/>  
  40.                 <Text style={{fontSize:20}}>If you like</Text>  
  41.                 <Button title={'Button 15'} onPress={this.onPressButton}/>  
  42.                 <Text style={{fontSize:20}}>Scrolling down</Text>  
  43.                 <Button title={'Button 16'} onPress={this.onPressButton}/>  
  44.             </ScrollView>  
  45.         );  
  46.     }  
  47. }  
  48. // skip this line if you are using Create React Native App  
  49. AppRegistry.registerComponent('AwesomeProject', () => ScrolledViewExample);  

Output:

React Native ScrollView React Native ScrollView

React Native Horizontal ScrollView Example

The horizontal ScrollView scrolls the components and views from left to right. It will be implemented by setting the props horizontal to true (horizontal={true}).

App.js

  1. import React, { Component } from 'react';  
  2. import { AppRegistry, ScrollView, View, Image, Text, Button, StyleSheet } from 'react-native';  
  3.   
  4. export default class ScrolledViewExample extends Component {  
  5.     onPressButton() {  
  6.         alert('You clicked the button!')  
  7.     }  
  8.   
  9.     render() {  
  10.         return (  
  11.             <ScrollView  horizontal={true} style={styles.container}>  
  12.                 <Text style={{fontSize:22, padding: 10}}>Horizontal ScrollView</Text>  
  13.                 <View style={[{ width: 220,height: 70,padding: 10 }]}>  
  14.                     <Button  
  15.                         onPress={this.onPressButton}  
  16.                         title="Button 1"  
  17.                         color="#FF3D00"  
  18.                     />  
  19.                 </View>  
  20.                 <Text style={{fontSize:22, padding: 10}}>javatpoint</Text>  
  21.                 <View style={[{ width: 220,height: 70,padding: 10 }]}>  
  22.                     <Button  
  23.                         onPress={this.onPressButton}  
  24.                         title="Button 2"  
  25.                         color="#3D00FF"  
  26.                     />  
  27.                 </View>  
  28.                 <Text style={{fontSize:22, padding: 10}}>React Native ScrollView Example</Text>  
  29.                 <View style={[{ width: 220,height: 70,padding: 10 }]}>  
  30.                     <Button  
  31.                         onPress={this.onPressButton}  
  32.                         title="Button 3"  
  33.                         color="#FFFF3D"  
  34.                     />  
  35.                 </View>  
  36.                 <Text style={{fontSize:22, padding: 10}}>If you like</Text>  
  37.                 <View style={[{ width: 220,height: 70,padding: 10 }]}>  
  38.                     <Button  
  39.                         onPress={this.onPressButton}  
  40.                         title="Button 4"  
  41.                         color="#FF3DFF"  
  42.                     />  
  43.                 </View>  
  44.                 <Text style={{fontSize:22, padding: 10}}>Scrolling horizontal</Text>  
  45.                 <View style={[{ width: 220,height: 70,padding: 10 }]}>  
  46.                     <Button  
  47.                         onPress={this.onPressButton}  
  48.                         title="Button 5"  
  49.                         color="#3DFF00"  
  50.                     />  
  51.                 </View>  
  52.             </ScrollView>  
  53.         );  
  54.     }  
  55. }  
  56. const styles = StyleSheet.create({  
  57.     container:{  
  58.         flex: 1,  
  59.     },  
  60. /*    buttonStyle:{ 
  61.         height: 50, 
  62.         width: 70, 
  63.     }*/  
  64. })  
  65. // skip this line if you are using Create React Native App  
  66. AppRegistry.registerComponent('AwesomeProject', () => ScrolledViewExample);  

Output:

React Native ScrollView React Native ScrollView

Comments

  1. The details that you mentioned regarding scroll view in react native is really great. I have gone through all the screenshots and the code also that you attached as sample and I believe that nothing could explain better than this.
    I was searching for react native development company and got your blog.
    Thanks sharing such a great blog.

    ReplyDelete

Post a Comment

Thank You.

Popular Posts

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...