All Projects → glamboyosa → mey

glamboyosa / mey

Licence: other
A react package that exports hooks for handling the request lifecycle.

Programming Languages

typescript
32286 projects
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to mey

Swr
React Hooks for data fetching
Stars: ✭ 20,348 (+112944.44%)
Mutual labels:  fetch, hooks, data-fetching
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+135605.56%)
Mutual labels:  fetch, hooks
fetchye
✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Stars: ✭ 36 (+100%)
Mutual labels:  fetch, data-fetching
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (+1483.33%)
Mutual labels:  fetch, hooks
React Fetching Library
Simple and powerful API client for react 👍 Use hooks or FACCs to fetch data in easy way. No dependencies! Just react under the hood.
Stars: ✭ 561 (+3016.67%)
Mutual labels:  fetch, hooks
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (+372.22%)
Mutual labels:  fetch, hooks
Tipple
A lightweight dependency-free library for fetching data over REST with React.
Stars: ✭ 133 (+638.89%)
Mutual labels:  fetch, hooks
React Infinite Scroll Hook
A simple hook to create infinite scroll list components
Stars: ✭ 151 (+738.89%)
Mutual labels:  fetch, hooks
use-async-resource
A custom React hook for simple data fetching with React Suspense
Stars: ✭ 92 (+411.11%)
Mutual labels:  fetch, data-fetching
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (+144.44%)
Mutual labels:  fetch
react-fusionui
☢️ Nuclear power-up for your UI.
Stars: ✭ 13 (-27.78%)
Mutual labels:  hooks
legible
the cleanest way to make http requests in js / node
Stars: ✭ 49 (+172.22%)
Mutual labels:  fetch
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (+5.56%)
Mutual labels:  hooks
react-use-hoverintent
React hook for hoverIntent
Stars: ✭ 16 (-11.11%)
Mutual labels:  hooks
react-web-editor
The react-web-editor is a WYSIWYG editor library. you can resize and drag your component. It also has simple rich text editor
Stars: ✭ 191 (+961.11%)
Mutual labels:  hooks
git-cheatsheet
One stop guide to help solve all your doubts related to Git & GitHub.
Stars: ✭ 31 (+72.22%)
Mutual labels:  fetch
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-16.67%)
Mutual labels:  fetch
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+1266.67%)
Mutual labels:  hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+261.11%)
Mutual labels:  hooks
sapper-httpclient
An isomorphic http client for Sapper
Stars: ✭ 48 (+166.67%)
Mutual labels:  fetch

mey

A react package that exports hooks for handling the request lifecycle

NPM JavaScript Style Guide

Motivation

This package was created for people who don't want to go through the chore of handling the request lifecycle but don't want to reach for a big data fetching library. A lot of people do not need the complexity of a big library, the project's aren't complex enough to warrant such overhead but equally are tired of handling the request lifecycle and the questions that come with it such as:

  • "should i use useReducer or useState ?"
  • "do i have different slices of state or a state object? which is cleaner?" This package aims to take all that pain away and exports two hooks to handle it all useFetch and useMutation

Install

npm install --save mey
yarn add mey

it is written in TypeScript so no need to install types.

useFetch Usage

import { useFetch } from "mey";

const { data, loading, error, refetch } = useFetch(
  "https://jsonplaceholder.typicode.com/posts"
);
console.log("the data is:");
console.log(data);
if (!data && loading) {
  return <div> loading </div>;
}
if (error) {
  return <div> {error} </div>;
}
return <div>{data.map((el: any) => el.title)[0]}</div>;

useMutation Usage

import  {useMutation}  from "mey";

const { data, loading, error, handleRequest } = useMutation(
  "https://jsonplaceholder.typicode.com/posts", "post"
);
const submitHandler = () => {
const randomNumber = Math.random() * 100;
const body = {
  randomNumber
}
handleRequest(body);
};
return (
  <div>
  <p> generate a new random number: {data && !error ? data : error } </p>
  <button disabled={loading} onClick={submitHandler}> click me </button>
)

Typing the response

If you want your response typed both useFetch and useMutation accept a generic in which you'd pass in the type. You can view in example/src/index.tsx or an example useFetch implementation down below:

import { useFetch } from "mey";
const { data, loading, error, refetch } = useFetch<
  { body: string; userId: number; id: number; title: string }[]
>("https://jsonplaceholder.typicode.com/posts");
console.log("The data is:");
console.log(data);
if (!data && loading) {
  return <div> loading </div>;
}
if (error) {
  return <div> {error} </div>;
}
return <div>{data.map((el) => el.title)[0]}</div>;

Global Config

Mey ships with a provider called MeyProvider that you would wrap around <App/> or <Component/> in your root, entry point of your project as the case may be. MeyProvider accepts a single prop BaseURL that is the primary URL you would be making calls to. the point is to eliminate typing the same base path in every component that uses a hook. you'd simply now pass the path you're trying to hit e.g "/posts" which would translate to "https://yourbasepath.com/posts"

Global Config Example

import "./index.css";
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { MeyProvider } from "mey";
ReactDOM.render(
  <MeyProvider BaseURL="https://yourbasepath.com">
    <App />
  </MeyProvider>,
  document.getElementById("root")
);

Extended useFetch Usage

import { useFetch } from "mey";

const { data, loading, error, refetch } = useFetch(
  "https://jsonplaceholder.typicode.com/posts",
  {
    authorization: " bearer authentication-token",
    xpth: "xsssf",
  }
);
if (!data && loading) {
  return <div> loading </div>;
}
if (error) {
  return <div> {error} </div>;
}
return <div>{data.map((el: any) => el.title)[0]}</div>;

Extended useMutation Usage

import  {useMutation}  from "mey";

const { data, loading, error, handleRequest } = useMutation(
  "https://jsonplaceholder.typicode.com/posts", "post", {
    authorization: " bearer authentication-token",
    xpth: "xsssf",
  }
);
const submitHandler = () => {
const randomNumber = Math.random() * 100;
const body = {
  randomNumber
}
handleRequest(body);
};
return (
  <div>
  <p> generate a new random number: {data && !error ? data : error } </p>
  <button disabled={loading} onClick={submitHandler}> click me </button>
)

useFetch API

const { data, loading, error, refetch } = useFetch(url, headers);

Parameters

  • url: the URL path you want to fetch.
  • headers: (optional) an object representing the values you want to set on the request header.

Values

  • data: data for the given path.
  • loading: a boolean representing whether the request is loading or not.
  • error: a string representing a potential error thrown.
  • refetch: a function that refetches data.

useMutation API

const { data, loading, error, handleRequest } = useMey(
  url,
  requestType,
  headers
);

Parameters

  • url: the URL path you want to fetch.
  • requestType: a union of string types representing the type of mutation you want to carry out i.e put, post & delete.
  • headers: (optional) an object representing the values you want to set on the request header.

Values

  • data: data for the given path.
  • loading: a boolean representing whether the request is loading or not.
  • error: a string representing a potential error thrown.
  • handleRequest: a function that handles dispatching requests. it accepts a body value. if the body value is not an object it stops execution and prints an error message to the console.

Support

Have a question ? send me an email @ [email protected] or hit me up on twitter. also feel free to checkout my portfolio

License

MIT © glamboyosa

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].