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

@vilem/rcr

v1.2.5

Published

React call rest - typesafe react hooks for simple calling rest api.

Downloads

8

Readme

RCR

React call rest

This library, which contains a set of React hooks, was created to simplify the API calls from React components and to enhance the developer experience through its universality, simplicity, and built-in TypeScript support.

The library is suitable for applications with dynamic data rather than those with static data. In the future, WebSocket support will definitely be added.

Feel free to open a pull request, issue or leave a feedback.

Installation

npm

npm i @vilem/rcr

yarn

yarn add @vilem/rcr

Content

Hooks:

  • useFetchApi - fetch data on component mount
  • useLazyFetchApi - fetch data after calling callback
  • useMutateApi - send tata to server

Components:

  • RCRProvider - component for wrapping applicatio with default RCR config

Usage

useFetchApi

The useFetchApi hook simplifies calling asynchronous APIs in React and automatically revalidates data upon argument changes. It's ideal for fetching data for display in an application and executes the fetcher function immediately upon mounting. It improves performance and is valuable for building dynamic user interfaces.

Example

You need to define a fetcher function, the fetch api, axios or whatever you want can be used in the fetcher function.

// userApi.ts
export interface User {
  name: string
}

export const getUser = async (userId: number): Promise<User> => {
  return (await axios.get(`/users/${userId}`)).data;
}

Using hook in component:

// User.tsx
import { getUser } from './userApi.ts';
import { useFetchApi } from '@vilem/rcr';

export default User = () => {
  const request = useFetchApi(getUser, 1);

  // a configuration object can also be used
  const requestWithOptions = useFetchApi(getUser, {
    revalidateOnArgsChange: false,
    revalidateOnFocus: false
  }, 1)

  if (request.loading || request.revalidating) return (
    <div>
      Loading...
    </div>
  )

  return (
    <div>
      <p>User name: {request.data.name}</p>
    </div>
  )
}

The request object:

  • call - Function that reloads all data
  • loading - Boolean that indicates the first loading of data
  • data - This is the returned value of fetcher function
  • errorMessage - Error message which is thrown by fetcher function
  • revalidate - Function which performs like call but loading is false while revalidating
  • revalidating - Boolean that indicates revalidation

useMutateApi

The useMutateApi hook is suitable for sending data to server.

Example

Again we need a fetcher function, but now it register nwe user.

// userAPi.ts

// ...

export const registerUser = async (userName: string): Promise<void> => {
  await axios.post('/users', { userName });
}

Using hook in component:

// RegisterUser.tsx
import { registerUser } from './userApi.ts';
import { useMutateApi } from '@vilem/rcr';
import { useState } from 'react';

export default User = () => {
  const [name, setName] = useState<string | null>(null);
  const request = useMutateApi(registerUser);

  const handleSubmit = async () => {
    const {data, errorMessage} = await request.call(name);

    console.log(data);
    console.log(errorMessage);
  }

  return (
    <div>
      <span>User name</span>
      <input type="text" onChange={(e) => {setName(e.target.value)}} value={name} />
      <button
        onClick={handleSubmit}
      >
        {request.loading ? 'Sending...' : 'Send'}
      </button>
    </div>
  )
}

The request object:

  • call - Function that reloads all data
  • loading - Boolean that indicates the first loading of data
  • data - This is the returned value of fetcher function
  • errorMessage - Error message which is thrown by fetcher function

Note: Currently the useMutateApi hook works the same as useLazyFetchApi.