m3k_1
m3k_1

Reputation: 417

Swipe down do refresh on regular View in react native app

Is there a way to do a swipe down to refresh in react native on a regular view without using something like a FlatList or ScrollView ?

Upvotes: -1

Views: 5493

Answers (2)

Hitesh Dalvi
Hitesh Dalvi

Reputation: 1

This is one of the ways to add refresh. I wrapped the view above the flatlist to implement the refresh

<View style={[styles.wrapper]}>
          <ScrollView
            scrollEnabled={false}
            nestedScrollEnabled={true}
            refreshControl={
              <RefreshControl
                refreshing={refreshing}
                onRefresh={onRefresh}
                tintColor={palette.blue}
                colors={[palette.blue, palette.green, palette.red]}
              />
            }>
            {status === 'draft' && (
              <TouchableOpacity
                style={[styles.editInfo]}
                onPress={() => {
                 //code to handle on press
                }}>
                <Text style={[styles.editText]}>
                  {nomenclature.edit}
                </Text>
                <CustomIcon
                  name={'edit'}
                  type={'Feather'}
                  size={14}
                  color={palette.blue_1}
                />
              </TouchableOpacity>
            )}

            <DetailsSection data={data} list={list} />
          </ScrollView>

          <FlatList
            data={list}
            renderItem={renderItem}
            keyExtractor={item => item?.id?.toString()}
            showsVerticalScrollIndicator={false}
            style={[cashCollectionDetailsStyles.list]}
            contentContainerStyle={[styles.listContainer]}
            ListEmptyComponent={
              <View style={[styles.emptyWrapper]}>
                <NoTaskFound title={nomenclature.no_applications_found} />
              </View>
            }
            windowSize={7}
            maxToRenderPerBatch={15}
            initialNumToRender={15}
            removeClippedSubviews={true}
          />
        </View>

Upvotes: 0

David Scholz
David Scholz

Reputation: 9856

There is no built-in solution for doing this. We usually want some kind of feedback for the user, thus the regular pull to refresh action is a suitable choice and this obviously only works in some kind of scrollable container.

However, if we still do not want to use a ScrollView for this purpose, then we could listen to touch gestures and react after a certain treshold as follows.

const SomeRefreshableNonScrollableComponent = (props) => {
   const y = useRef()

   function onRefresh() {
        // refresh or whatever
   }

   return (
    <View
      style={{flex :1}}
      onTouchStart={e=> y.current = e.nativeEvent.pageY}
      onTouchEnd={e => {
        // some threshold. add whatever suits you
        if (y.current - e.nativeEvent.pageY < 40) {
          onRefresh()
        }
      }}>

     ...

    </View>
  )
}

Edit: As an answer to the comments.

We can implement a pull down to refresh mechanism for a FlatList that is nested inside a not scrollable ScrollView as follows.

const [data, setData] = useState([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  const [refresh, setRefresh] = useState(false)

  useEffect(() => {

    if (refresh) {
      console.log("Refresh my inner data on pull")
    }
  }, [refresh])

  return (
    <ScrollView scrollEnabled={false} nestedScrollEnabled={true}>
      <FlatList refreshing={refresh} onRefresh={() => setRefresh(true)} data={data} renderItem={({item}) => {
        return <Text>Hello world</Text>
      }} />
    </ScrollView>
  );
};

Notice that you must reset your refresh state back to false once your refresh action is done, e.g. if an API call returns its data. Otherwise, the Flatlist will keep showing the refresh indicator.

Upvotes: 1

Related Questions