• Stars
    star
    285
  • Rank 145,115 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React Native/React Navigation compatibility for Vercel's useSWR hook. ๐Ÿฎ

SWR + React Native ๐Ÿฎ

Add React Native + React Navigation compatibility to swr. ๐Ÿ‘จ๐Ÿปโ€๐Ÿ”ง

- import useSWR from 'swr'
+ import useSWRNative from '@nandorojo/swr-react-native'

That's it.

SWR revalidation now works in your React Native app. Requests also revalidate when your React Navigation screens focus.

Why?

swr is an awesome data fetching + caching library by Vercel.

However, some of its essential features, such as revalidateOnFocus & revalidateOnConnect, don't work on React Native.

This library provides a simple drop-in replacement for useSWR, which adds compatibility for React Native / React Navigation.

It comes with 2 hooks: useSWRNative, and useSWRNativeRevalidate.

Features

  • Adds support for revalidateOnConnect & revalidateOnFocus.
  • Configurable focusEventThrottle
  • Web, iOS and Android support
  • Zero config
  • Revalidates when AppState becomes active
  • Works with React Navigation, revalidating on screen focus
  • TypeScript support
  • useSWRInfinite support

Installation

yarn add @nandorojo/swr-react-native

Next, install peer dependencies:

# if you're using expo
expo install @react-native-community/netinfo

# if you aren't using expo
yarn add @react-native-community/netinfo

Usage with SWR v1

Currently, SWR v1 is in beta:

yarn add swr@beta

To use swr-react-native, you can install with @beta too:

yarn add @nandorojo/swr-react-native@beta

Usage

There are 2 ways to use this library:

1. Simplest usage

Replace imports of useSWR with useSWRNative. That's it!

import useSWRNative from '@nandorojo/swr-react-native'

const { data, mutate, error } = useSWRNative(key, fetcher, config)

2. Custom usage

If, for some reason, you don't want to replace your imports, you can use the useSWRNativeRevalidate hook. This allows you to de-couple the revalidation from the useSWR hook itself.

This option exists in case useSWR makes some big changes to their API or something down the line. Or, maybe you're using React Native web, and not all screens are nested in a React Navigation stack, so you want to call this only in those places.

If you're using useSWRInfinite, then this is the method for you.

import { useSWRNativeRevalidate } from '@nandorojo/swr-react-native'

Call useSWRNativeRevalidate, likely below your useSWR function:

const { data, mutate } = useSWR(key, fetcher)

useSWRNativeRevalidate({
  // required: pass your mutate function returned by SWR
  mutate

  // optional, defaults copied from SWR
  revalidateOnFocus: true,
  revalidateOnReconnect: true,
  focusThrottleInterval: 5000,
})

The mutate function is required!

If you're using useSWRInfinite, this you should rely on this usage:

const { data, mutate } = useSWRInfinite(...)

useSWRNativeRevalidate({
  // required: pass your mutate function returned by SWR
  mutate
})

Context

I'm a big fan of SWR. I've also built a fetching library for Firebase/Firestore based on swr, which you can find here.

The idea for this library originated from this issue. Thanks to @te-online for help testing it.

More Repositories

1

moti

๐Ÿผ The React Native (+ Web) animation library, powered by Reanimated 3.
TypeScript
3,956
star
2

solito

๐Ÿงโ€โ™‚๏ธ React Native + Next.js, unified.
TypeScript
3,426
star
3

dripsy

๐Ÿท Responsive, unstyled UI primitives for React Native + Web.
TypeScript
1,918
star
4

zeego

Menus for React (Native) done right.
TypeScript
1,520
star
5

burnt

Crunchy toasts for React Native. ๐Ÿž
Java
1,140
star
6

swr-firestore

Implement Vercel's useSWR for querying Firestore in React/React Native/Expo apps. ๐Ÿ‘ฉโ€๐Ÿš’๐Ÿ”ฅ
TypeScript
763
star
7

expo-next-react-navigation

โ›ด Make Next.js and react-navigation play nicely together with an Expo/React Native Web app.
TypeScript
388
star
8

react-navigation-heavy-screen

โšก๏ธOptimize heavy screens to prevent lags during React Navigation transitions.
TypeScript
320
star
9

react-native-anchors

๐Ÿฆ… Anchor links and scroll-to utilities for React Native (+ Web)
JavaScript
299
star
10

galeria

The React (Native) Image Viewer. ๐Ÿ“ท
TypeScript
169
star
11

expo-next-monorepo

๐Ÿ’ธ Expo + Next.js monorepo starter.
JavaScript
114
star
12

expo-spotify-party

๐ŸŽน Listen to Spotify with your friends in real-time. (React Native Expo + Next.js)
TypeScript
88
star
13

nextjs-conf-22-example

Example repository for the 2022 Next.js Conf about React Native.
TypeScript
67
star
14

react-native-iconic

๐ŸŽจ Iconic icons for React Native (+Web)
TypeScript
65
star
15

react-native-heroicons

Hero Icons for React Native. ๐Ÿซก
TypeScript
64
star
16

zero-to-10m

Resources from my talk at Next.js Conf: "Zero to $10 Million with React Native + Next.js"
57
star
17

expo-firestore-offline-persistence

โ„๏ธ Enable Firestore offline persistence in Expo apps without detaching.
JavaScript
51
star
18

tamagui-intellisense

VSCode Plugin for Tamagui
JavaScript
41
star
19

sf-symbols-typescript

TypeScript types for iOS SF Symbols ๐Ÿ
TypeScript
36
star
20

doorman

๐ŸšชDoorman is a full-stack phone authentication solution for Expo + Firebase auth.
TypeScript
29
star
21

reanimated-tree-shaking

A Next.js playground to test Reanimated bundle size.
TypeScript
24
star
22

sticky

Implement position: sticky in React Native
TypeScript
15
star
23

ts-monorepo-autoimport-guide

A guide for making VSCode's autoimport work in a monorepo.
12
star
24

expo-gatsby-navigation

๐ŸคตMake Gatsby and React Navigation play nicely together with an Expo/React Native Web app.
TypeScript
12
star
25

tgui

TypeScript
11
star
26

react-native-2030

The implementation of my App.js Conf talk, "React Native 2030"
11
star
27

fuego

๐Ÿ”ฅFirebase Firestore hooks & components for React Native/React.
TypeScript
10
star
28

shared-animations

๐Ÿ•บA global state manager for React Native animations.
JavaScript
10
star
29

expo-electron-copy-magic

Copy clip sucks, so this is a better one made with Expo + Electron.
TypeScript
10
star
30

lint-expo

๐Ÿ  A mini script that configures prettier and eslint for React/Expo projects.
JavaScript
9
star
31

tamagui-starter

Starter project for issue reproductions on CodeSandbox
TypeScript
8
star
32

doorman-examples

๐ŸฅณA repository of example apps for Doorman (Expo + Firebase phone auth)
TypeScript
8
star
33

solito-test

TypeScript
6
star
34

react-doorman

React Core library for Doorman's Firebase Auth, without the RN dependencies. ๐Ÿ‘พMeant for use in Gatsby/other React apps.
TypeScript
4
star
35

expo-eas-semantic-release

git push โ†’ automated Expo EAS builds, over-the-air updates, & prerelease versions.
JavaScript
4
star
36

lexical-ios-react-native

Just an experiment with Expo Modules
Swift
4
star
37

expo-starter-project

eslint, src folder, etc
TypeScript
3
star
38

panda

๐Ÿผ Design websites in seconds. Export clean code. (PRE-ALPHA)
JavaScript
3
star
39

expo-navigation-core

Package with reusable expo web navigation hooks / components.
TypeScript
2
star
40

lint-react-native-app

๐Ÿค“ Simple command to add ESLint and Prettier to a react-native project.
TypeScript
2
star
41

thet

TypeScript
2
star
42

animateMePlz

โšก๏ธ A lightweight jQuery extension for scrolling animations.
HTML
2
star
43

expo-router-bug

TypeScript
1
star
44

nandorojo

1
star
45

plaid-bug-repro

Repro for https://github.com/plaid/react-native-plaid-link-sdk/issues/436
Objective-C
1
star
46

expo-next-issue

โšก๏ธReact-native-paper issue with expo / next.
JavaScript
1
star
47

react-native-bootstrap

React-bootstrap, remade for React Native (& React Native For Web).
TypeScript
1
star
48

reanimated-next13-issue

Reanimated isn't working with Next.js 13.
JavaScript
1
star
49

react-native-beacons-example

Repo for react-native-beacons
TypeScript
1
star
50

react-spring-next-issue

Reproducible repo for this issue: https://github.com/react-spring/react-spring/issues/627#issuecomment-541235141
TypeScript
1
star
51

next-style-issue

Issue: prop did not match, server: x, client: y for issue: https://github.com/vercel/next.js/discussions/14469
TypeScript
1
star
52

writing-javascript-actions

1
star
53

eslint-config-nando

Stress-free react native linting. ๐Ÿ‹๐Ÿปโ€โ™‚๏ธ
JavaScript
1
star
54

Penn-Requirement-Counter

๐Ÿค“ Find courses that count for one or more requirements at UPenn.
JavaScript
1
star
55

expo-web-img-picker-bug

SDK 41 image picker not working on web.
JavaScript
1
star
56

expo-next-webpack5-issue

Reproduction of Webpack 5 not working with Expo in a Next.js app
JavaScript
1
star