1

I am trying to get overflow scroll working with my React Native app using NativeWind CSS, which is the native version of Tailwind css.

I am relatively inexperienced with CSS, so I am really struggling to get the below to work.

I have stripped my app back to basically nothing with the three files below in order to try and get this to work and understand where I am going wrong but still no success.

import { StatusBar } from "expo-status-bar";
import { Text, View, SafeAreaView } from "react-native";
import Box from "./components/box";
import SafeViewAndroid from "./SafeViewAndroid";

export default function App() {
  return (
    <SafeAreaView style={SafeViewAndroid.AndroidSafeArea}>
      <StatusBar style="auto" />
      <View className="h-screen overflow-scroll">
        {[1, 2, 3, 4, 5, 6, 7, 8, 9].map((_, i) => (
          <Box key={i} />
        ))}
      </View>
    </SafeAreaView>
  );
}
import React from "react";
import { View, Text } from "react-native";

const Box = () => {
  return (
    <View className="flex-1 h-10 w-screen border-2 border-gray-800 items-center justify-between">
      <Text>Box</Text>
    </View>
  );
};

export default Box;
import { StyleSheet, Platform, StatusBar } from "react-native";

export default StyleSheet.create({
  AndroidSafeArea: {
    flex: 1,
    backgroundColor: "#FFF",
    paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0,
  },
});

4
  • 1
    Use reactnative.dev/docs/scrollview Commented Jan 14, 2023 at 9:04
  • I appreciate the response @Karthikeyan but I am trying to use tailwind css / nativewind css to stay consistent with the project Commented Jan 14, 2023 at 9:11
  • 1
    Hi Ry2254, @Karthikeyan is right. Check my below answer and let me know please :) Commented Jan 14, 2023 at 9:50
  • 1
    @Karthikeyan ScrollView is the answer. I got thrown off by seeing the StyleSheet in the documentation but StyleSheet isn't needed in order the scroll. Thanks for your help. Commented Jan 14, 2023 at 10:02

2 Answers 2

4

Scrolling in React Native is slightly different than in the browser. It's not a styling issue, there is this ScrollView that needs to wrap the things where you wanna have a scroll, as an example:

import { StatusBar } from "expo-status-bar";
import { ScrollView, View, SafeAreaView } from "react-native";
import Box from "./components/box";
import SafeViewAndroid from "./SafeViewAndroid";

export default function App() {
  return (
    <SafeAreaView style={SafeViewAndroid.AndroidSafeArea}>
      <StatusBar style="auto" />
      <ScrollView>
        {[1, 2, 3, 4, 5, 6, 7, 8, 9].map((_, i) => (
          <Box key={i} />
        ))}
      </ScrollView>
    </SafeAreaView>
  );
}

If you have any trouble with this implementation, visit the doc by clicking on the above link. They talk about the edge cases.

And if you have a really long list to scroll, it's preferable to use FlatList.

Sign up to request clarification or add additional context in comments.

Comments

0

So as mentioned about the answer was to use ScrollView. I have an example below of what fixed it.

If you want to scroll horizontal then just use "horizontal" keyword in the ScrollView below.

import React from "react";
import { ScrollView, Text, View } from "react-native";

const Box = () => {
  return (
    <ScrollView horizontal>
      <View className="flex flex-row">
        <Text className="h-10 w-48 bg-slate-500">Box</Text>
        <Text className="h-10 w-48 ">Box</Text>
        <Text className="h-10 w-48 bg-slate-500">Box</Text>
        <Text className="h-10 w-48 bg-slate-500">Box</Text>
        <Text className="h-10 w-48 ">Box</Text>
      </View>
    </ScrollView>
  );
};

export default Box;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.