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-network-component

v2.0.0

Published

Easily add online/offline and resume events to fetch data into your components

Downloads

6

Readme

react-native-network-component

react-native-network-component

What's the point ?

This class allows you to easily fire your data fetching method when the user's network is back online or when he's resuming the app, making it easy to refresh the data or to download it as soon as possible.

Handling a disturbed and changing network is primordial for the user's experience.

How to use

The first step is to extend your component class with this one instead of React's Component one.

You also need to call super.componentDidMount() and super.componentWillUnmount() in these lifecycle methods if you're already implementing them. If you don't, skip this step.

This class will provide you with the handleNetwork() method that you should use to attach online/offline listener when you want to. A good practice would be to use it when you couldn't fetch the data.

That's about it, here's an example :

import React, { PropTypes } from 'react';
import NetworkComponent from 'react-native-network-component';
import { fetchAPI } from 'cpacollecte/src/services';

// Don't forget to extend with NetworkComponent
export default class Article extends NetworkComponent {

    constructor (props) {
        super(props);
        // In this example, 1 = loading, 2 = ok, 3 = error
        this.state = {
            status: 1
        };
    }

    componentDidMount () {
        // I'm already implementing componentDidMount in this class,
        // so I need to call the one from NetworkContainer
        super.componentDidMount();
        this.fetchData();
    }

    // My method responsible for fetching data
    async fetchData () {
        const { _id } = this.props;

        try {
            // Display the loader again
            if (!this.state.status !== 1) {
                this.setState({ status: 1 });
            }
            const data = await fetchAPI('articles', { params: { _id } });
            if (data) {
                this.setState({ data, status: 2 });
            } else {
                this.onError();
            }
        } catch (err) {
            console.warn(err);
            this.onError();
        }
    }

    onError () {
        this.setState({ status: 3 });
        // I'm firing handleNetwork only when the data couldn't be
        // fetched to avoid attaching an useless eventListener
        this.handleNetwork();
    }

    // and all the rendering stuff...
}

In summary, if the data couldn't be fetched when the component has been mounted, the onError and more importantly the handleNetwork methods will be fired. This way, when the user is back online, fetchData will be fired again.

In addition, resuming the app from the background will also fire fetchData, but this is customizable.

Options

This class has three options, here's how you can change them :

    constructor (props) {
        super(props, { fetchFunc: 'myFetchingMethod', handleAppState: false });
    }

Option | Description | Type | Default ------ | ------ | ------ | ------ fetchFunc | The name of the method that needs to be fired | string | fetchData handleAppState | Fire the method when resuming the app | bool | true onResumeDelay | Delay (in ms) that the apps needs to stay in background to trigger fetchFunc when resuming | number | 1000 * 60 * 5 (5')

Some notes

The networking and data handling part can be extremely different from one app to the other. This little class is designed to match our needs, so it might lack features to integrate it in specific situations.

Feel free to open an issue and submit a PR to extend its functionalities.