0.0.2 • Published 5 years ago

@softbind/hook-use-error v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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>
);