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

useapicalling

v1.0.1

Published

a hook for ease api calling and its state managment

Downloads

5

Readme

useapicalling Hook

The useapicalling hook is a custom React Native hook designed to handle API calls with built-in state management for loading, error handling, and data manipulation.

Installation

No additional installation is required for the useapicalling hook itself, but ensure you have the necessary dependencies installed for your React Native and Redux environment.

Usage

import useapicalling from 'path-to-your-hook';
import { fetchSomeData } from 'path-to-your-api-calls';

const MyComponent = () => {
  const { data, loading, error, fetchData, alterData } = useapicalling({
    apiCallingFunction: fetchSomeData,
    apiParameters: [/* your parameters here */],
    runOnTimeOfScreenMount: true, // optional
    initialLoadingState: true,    // optional
    initialData: [],              // optional
    reFetchDependencies: [/* dependencies for refetching */] // optional
  });

  return (
    <View>
      {loading && <Text>Loading...</Text>}
      {error && <Text>Error: {error}</Text>}
      <Text>Data: {JSON.stringify(data)}</Text>
      <Button title="Refetch Data" onPress={() => fetchData(true, [/* new parameters */])} />
    </View>
  );
};

export default MyComponent;

Arguments

apiCallingFunction

  • Type: (...params: any[]) => Promise<any>
  • Description: The function that makes the API call. It should return a promise.
  • Required: Yes

apiParameters

  • Type: any[]
  • Description: Parameters to be passed to the apiCallingFunction.
  • Required: Yes

apiCustomReturnFunction

  • Type: (data: any) => any
  • Description: Custom function to transform the API response data before updating the state.
  • Required: No

runOnTimeOfScreenMount

  • Type: boolean
  • Description: If true, the API call will be made when the component mounts.
  • Required: No

initialLoadingState

  • Type: boolean
  • Description: Initial loading state of the hook.
  • Required: No

initialData

  • Type: any
  • Description: Initial data state of the hook.
  • Required: No

reFetchDependencies

  • Type: any[]
  • Description: Dependencies array for re-fetching data when any of these dependencies change.
  • Required: No

Example

import React, { useEffect } from 'react';
import { View, Text, Button } from 'react-native';
import useapicalling from 'path-to-your-hook';
import { fetchUserData } from 'path-to-your-api-calls';

const UserProfile = () => {
  const { data, loading, error, fetchData, alterData } = useapicalling({
    apiCallingFunction: fetchUserData,
    apiParameters: [userId],
    runOnTimeOfScreenMount: true,
    initialLoadingState: true,
    initialData: {},
    reFetchDependencies: [userId]
  });

  useEffect(() => {
    if (error) {
      console.error('API Error:', error);
    }
  }, [error]);

  return (
    <View>
      {loading && <Text>Loading user profile...</Text>}
      {error && <Text>Error loading profile: {error}</Text>}
      <Text>User Data: {JSON.stringify(data)}</Text>
      <Button title="Reload Profile" onPress={() => fetchData(true, [userId])} />
    </View>
  );
};

export default UserProfile;

Functions Returned by the Hook

fetchData

  • Description: Function to manually trigger the API call. Optionally, you can pass new parameters and a custom return function.
  • Arguments:
    • showLoader (boolean): If true, sets loading state before making the API call.
    • params (any[]): Optional new parameters for the API call.
    • customReturnFunc ((data: any) => any): Optional custom return function to process the response data.

alterData

  • Description: Function to manually alter the data state.
  • Arguments:
    • data (any): The new data to set in the state.

Contributing

Created by Chandan Nath