1.0.2 • Published 3 years ago

babel-custom-hooks v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

[licence: MIT

Babel Custom Hooks

My personal custom hooks and a compilation of comunity custom hooks

Prerequisites

This project requires NodeJS (version 14.16.1 or later) and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following command.

$ npm -v && node -v
7.19.1
v14.16.1

Table of contents

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Installation

BEFORE YOU INSTALL: please read the prerequisites

Start with cloning this repo on your local machine:

$ git clone https://github.com/Sstark97/babel-custom-hooks
$ cd PROJECT

To install and set up the library, run:

$ npm install -S myLib

Or if you prefer using Yarn:

$ yarn add --dev myLib

Usage

useFetch

useFetch(url: string = '', config: object)

Options

url

TypeDefault value
string''

If present, the request will be performed as soon as the component is mounted

Example:

const FetchComponent = () => {
  const { data, error, loading } = useBasicFetch(
    "https://api.icndb.com/jokes/random"
  );

  if (error) {
    return <p>Error</p>;
  }

  if (loading) {
    return <p>Loading...</p>;
  }

  return (
    <div className="App">
      <h2>Chuck Norris Joke of the day</h2>
      {data && data.value && <p>{data.value.joke}</p>}
    </div>
  );
};

config

TypeDefault valueDescription
ObjectnullFecth optional Config

If present, the request will be use the optional config

Example:

const FetachComponent = () => {
  const { data, error, loading } = useFetch(
    "https://api.icndb.com/jokes/random",
    {
      method: "POST",
      body: {
        name: "I'm POST",
      },
    }
  );

  if (error) {
    return <p>Error</p>;
  }

  if (loading) {
    return <p>Loading...</p>;
  }

  return (
    <div className="App">
      <h2>Chuck Norris Joke of the day</h2>
      {data && data.value && <p>{data.value.joke}</p>}
    </div>
  );
};

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Add your changes: git add .
  4. Commit your changes: git commit -am 'Add some feature'
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request :sunglasses:

Authors

License

MIT