@softbind/hook-use-error
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

useError

Installation

npm i @softbind/hook-use-error --save

API

useError()

import React, { useEffect } from "react";
import { useError } from "@softbind/hook-use-error";

class ErrorBoundary extends React.Component {
  state = { hasError: false }

  componentDidCatch(error, info) {
    // Display fallback UI
    this.setState({ hasError: true });
    // You can also log the error to an error reporting service
    logErrorToMyService(error, info);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }
    return this.props.children;
  }
}

const Fetcher = ({ url }) => {
  const [dispatchError] = useError()

  useEffect(() => {
    fetch(url).catch(dispatchError)
  }, [])
  
  return (
	<span>Loading...</span>
  )
}

const App = ({ children }) => (
  <ErrorBoundary>
    <Fetcher url="https://google.pl" />
  </ErrorBoundary>
);

Package Sidebar

Install

npm i @softbind/hook-use-error

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

3.85 kB

Total Files

9

Last publish

Collaborators

  • hiddenboox