npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

swr-react-native

v0.1.2

Published

Add React Native/React Navigation compatibility to [`swr`](https://swr.vercel.app). ๐Ÿ‘จ๐Ÿปโ€๐Ÿ”ง

Downloads

167

Readme

SWR + React Native ๐Ÿฎ

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

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

That's all. 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 compatibility.
  • Zero config
  • Works with React Navigation

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

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 option exists in case useSWR makes some big changes to their API or something down the line.

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

Call useSWRNativeRevalidate, likely below your useSWR function:

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

useSWRNative({
  // required: pass your revalidate function returned by SWR
  revalidate

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

The revalidate function is required!

Context

The idea for this library originated from this issue.

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.

It's still pretty new, and isn't super battle tested, so I'd appreciate help testing it.