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

react-native-once

v1.2.1

Published

Run code once or multiple time and persist the preference with a declarative API in your React Native applications.

Downloads

5

Readme

react-native-once

Run code once or multiple times based on user preference. Uses AsyncStorage to track if code should be executed. This library is built simply to reduce the complexity introduced into components when running code conditionally based on application and user preferences. e.g.

  1. App first open -> show on-boarding experience
  2. Modal -> don't show again.

Before

let introValue = null;

try {
  introValue = await AsyncStorage.getItem("on-boarding");
} catch (error) {
  // The data does not exist.
}

if (!introValue) {
  Navigation.push("OnBoardingScreen");
  await AsyncStorage.setItem("on-boarding", "true");
}

After

<Once
  name="on-boarding"
  onSuccess={() => Navigation.push("OnBoardingScreen")}
/>

Install

Install via npm:

 npm install react-native-once --save

Usage

There is one component which allows you to use all the methods and declarative API.

 import { Once } from "react-native-once";

Component

Simply use the exported Once component with a unique key (name) and callbacks to run your code. List of props are available below.

<Once
  name="on-boarding"
  onSuccess={() => console.log("Show on-boarding screens")}
  onError={(e) => console.log(e)}
/>

API

The API can be accessed statically through the Once component. It allows you to access the value stored against the key and perform actions.

Gets the value belonging to the key:

Once.get("onboarding-intro", value => {
  // value -> true or false (has the on-boarding screen been shown).
  // This is useful if you needed to update UI e.g. checkbox based on this value.
});

Sets the value of the key:

// Allows you to update the key. Value must be a boolean.
Once.set("onboarding-intro", false);

Props

| Prop | Type | Optional | Description | | ------------------- | ------------- | --------- | --------------------------------------------------------------------------------------- | | name | string | No | A unique key provided to be used in Async Storage. | | onSuccess | func | No | Function executed if the key does not exist or key === true. | | onError | func | Yes | Function executed if there was an error accessing AsyncStorage. | | delay | number | Yes | Delay for rendering children, if you choose to render children in the once component. | | | auto | bool | Yes | Automatically toggle the key to true if the onSuccess function is executed. | | platform | string | Yes | "ios" / "android", should this only run on a specific platform? | | expire | number | Yes | UNIX Timestamp. Set an expiry for the key. |

Authors

License

This project is licensed under the MIT License