1.0.1 • Published 9 months ago

@shivshakti7/error-handler v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

code style: prettier

Error Handler Function

This is a simple error handler function for API calls, that is built to be used with ReactJS

Prerequisites

This project requires NodeJS (version 8 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
8.19.4
16.20.0

Table of contents

Getting Started

Just create a React app and start using this package with the installation commands

Installation

BEFORE YOU INSTALL: please read the prerequisites

Start with installing the package using npm:

npm install @shivshakti7/error-handler

Or if you prefer using Yarn:

$ yarn add @shivshakti7/error-handler

Usage

Serving the app

Options

props

The errorHandler() function requires a url parameter (url is the API point for GET request)

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

Example:

import { useState, useEffect } from 'react'
import './App.css'
import errorHandler from '@shivshakti7/error-handler'

interface Data {
  id: number
  brand: string
  description: string
  images: string[]
  price: number
  title: string
}

function App() {
  const [product, setProduct] = useState<Data[] | null>(null)

  useEffect(() => {
    errorHandler('https://dummyjson.com/products').then((data) => {
      setProduct(data.products)
    })
    .catch((error) => {
        console.log('Error: ', error)
      })
  }, [])

  return (
    <div className="App">
      {product?.map((item) => (
        <div key={item.id}>
          <h2>{item.id}</h2>
          <h2>{item.title}</h2>

          <h2>{item.brand}</h2>
          <h2>{item.description}</h2>
          <h2>{item.price}</h2>
          <p>
            {item.images.map((img) => (
              <img key={img} src={img} alt="products" />
            ))}
          </p>
        </div>
      ))}
    </div>
  )
}

export default App

Built With

  • ReactJS
  • NodeJS

Versioning

  • GitHub
  • GitLab

Authors

License

ISC