Search code examples
javascriptreactjsscrollviewreact-native

how to use scroll view in react native


i am trying to create a shopping app for a client and i wanted to add the scrolling property to the cart page of the app so i used the

`scrollview`

property, immediately i added the scrollview and reloaded the app everything scattered.

here is the original page without the scrollveiw

enter image description here

and here is the page with the scrollview

enter image description here

i fixed this problem after some research by adding this contentContainerStyle={{flex:1}}to the scrollview, after that i added more content to the page but it did not scroll, i don't know know why, maybe i might be doing something wrong here is the code for the page

 <ScrollView contentContainerStyle={{flex:1}}>
    <View style={styles.page}>
       <Cartheader gotocart={gotocart} gotohome={gotohome} />
       <View style={{justifyContent:'space-between', alignItems:'center'}}>
       <View style = {styles.up}>

        <TouchableOpacity style={styles.back} onPress={gtoback}>
            <Image style={styles.arrowb} source={require('../resources/arrowback.png')} />
        </TouchableOpacity>

        <View style={styles.overall}>
            <View style = {styles.title}><Text style={{fontSize:RFPercentage(5),fontWeight:'bold',marginLeft: '4%',}}>Cart</Text></View>

            <View style = {styles.cartitems}>
                <View style ={styles.details}>
                    <View style={styles.details1}><Text style={{fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Item</Text></View>
                    <View style={styles.details2}><Text style={{fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Quantity</Text></View>
                    <View style={styles.details3}><Text style={{fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Item Total</Text></View>
                    <View style={styles.details4}><Text style={{fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Remove</Text></View>
                </View>
                <View style ={styles.real}>
                <View style={styles.real1}>
                <Image style={styles.dem} source={require('../resources/comfortable-seats-in-empty-corporate-meeting-office-for-presentation-JCC1N0.jpg')} />
                <View style={styles.descrip}>
                    <View><Text style={{fontWeight:'bold',fontSize:RFPercentage(2.5)}}>A/c Chairs</Text></View>
                    <View><Text style={{fontSize:RFPercentage(1.25), color:'#40b9e9'}}>N 400.00 NGN</Text></View>
                </View>
                </View>
                <View style={styles.real2}>
                    <View style={{width:'50%',height:'60%',justifyContent:'space-around',alignItems:'center'}}><Text style = {{fontSize:RFPercentage(2.5)}}>2</Text></View>
                    <View style={{width:'50%',height:'60%',justifyContent:'space-between'}}>
                    <TouchableOpacity>
            <Image style={styles.arrowc} source={require('../resources/arrowback.png')} />
        </TouchableOpacity>

        <TouchableOpacity  >
            <Image style={styles.arrowd} source={require('../resources/arrowback.png')} />
        </TouchableOpacity>
                    </View>
                </View>
                <View style={styles.real3}><Text style={{color:'#40b9e9',fontSize:RFPercentage(1.5)}}>N 800.00 NGN</Text></View>
                <View style={styles.real4}>
                    <TouchableOpacity style={styles.x}>
                        <Text style={{color:'#fff',fontSize:RFPercentage(2.5),marginTop:-2}}>x</Text>
                    </TouchableOpacity>
                </View>
                </View>
            </View>
            {/* ///////////////////////////////////// */}
            <View style={styles.payment}>
                <View style={{justifyContent:'space-around',height:'100%',width:'57%'}}>
                    <View style={styles.pay}><Image style={styles.radio} source={require('../resources/radio.png')} />
                    <Text style={{marginLeft:5,fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Pay On Delivery</Text></View>
                    <View style={styles.pay}><Image style={styles.radio} source={require('../resources/radio.png')} />
                    <Text style={{marginLeft:5,fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Paystack(subscription)</Text></View>
                    <View style={styles.pay}><Image style={styles.radio} source={require('../resources/radio.png')} />
                    <Text style={{marginLeft:5,fontWeight:'bold',fontSize:RFPercentage(1.8)}}>Qucikteller(Master Card/Verve)</Text></View>
                </View>
                <View style={{justifyContent:'space-around',height:'100%',width:'41%'}}>
                    <View style = {styles.total}><Text style={styles.bold}>Sub Total:   </Text><Text style={{color:'#40b9e9',fontSize:RFPercentage(1.8)}}>N 7400.00</Text></View>
                    <View style = {styles.total}><Text style={styles.bold}>Shipping:   </Text><Text style={{color:'#40b9e9',fontSize:RFPercentage(1.8)}}>N 0.00</Text></View>
                    <View style = {styles.total}><Text style={styles.bold}>Garnd Total:   </Text><Text style={{color:'#40b9e9',fontSize:RFPercentage(1.8)}}>N 7400.00</Text></View>

                </View>
            </View>
            <View style={styles.terms}>
            <Image style={styles.checkbox} source={require('../resources/check.png')} />
            <Text style= {{fontSize:RFPercentage(1.8)}}>I Have Read And i Agree To The Terms & Conditions</Text>
            </View>
            <View style={styles.buttons}>
                <TouchableOpacity style={{width:'70%',height:'40%',backgroundColor:'darkblue',justifyContent:'space-around',alignItems:'center',}}>
                    <Text style={{color:'#fff',textAlign:'center',fontSize:RFPercentage(3.2), fontWeight:'bold'}}>Proceed To Checkout</Text>
                </TouchableOpacity>
                <TouchableOpacity style={{width:'70%',height:'40%',}}>
                    <Text style={{color:'darkblue',textAlign:'center',fontSize:RFPercentage(3.2), fontWeight:'bold'}} onPress={gotohome}>Back To Shop</Text>
                </TouchableOpacity>
            </View>
            <View style={{width:'30%',backgroundColor:'red'}}></View>
        </View>

       </View>
       </View>
    </View></ScrollView>

thanks in advance!!!


Solution

  • try copying what they do in the documentation: scrollView Doc

    Here is the code from their snack :

    export default function App() {
      return (
        <SafeAreaView style={styles.container}>
          <ScrollView style={styles.scrollView}>
            <Text style={styles.text}>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
              eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
              minim veniam, quis nostrud exercitation ullamco laboris nisi ut
              aliquip ex ea commodo consequat. Duis aute irure dolor in
              reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
              pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
              culpa qui officia deserunt mollit anim id est laborum.
            </Text>
          </ScrollView>
        </SafeAreaView>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        marginTop: Constants.statusBarHeight,
      },
      scrollView: {
        backgroundColor: 'pink',
        marginHorizontal: 20,
      },
      text: {
        fontSize: 42,
      },
    });
    

    The point you should note is that they are using flex:1 in the style attribute, not on contentContainerStyle.