react-use-async-loading

0.9.2 • Public • Published

react-use-async-loading

Made with create-react-library

NPM JavaScript Style Guide

Install

npm install --save react-use-async-loading

Usage

Easy way to manage bool state to block buttons or show spinner/load data Just decorate promise and the bool will automatically switch

import {useAsyncLoading} from "react-use-async-loading";


const Page = () => {
  const [loading, {registerPromise}] = useAsyncLoading(true) // by default - false;

  useEffect(() => {
    // Show spinner while fetching data from server
    registerPromise(
        fetch("/data.json")
        .then(data => {
        // ...save data to state
      })
    )
  }, [registerPromise])

  if (loading)return (<Spinner/>)

  return (<div>Data was loaded</div>)
}
import {useAsyncLoading} from "react-use-async-loading";


const Form = () => {
  const [saving, {registerPromise}] = useAsyncLoading();

  const onSubmit = values => {
    // disable submit button while doing request
    registerPromise(
      axios.post(`/register`, values)
    )
  }

  return (<Form disabled={saving} onSubmit={onSubmit} />)
}

License

MIT © Link to repository

Package Sidebar

Install

npm i react-use-async-loading

Weekly Downloads

4

Version

0.9.2

License

MIT

Unpacked Size

8.84 kB

Total Files

7

Last publish

Collaborators

  • max_ivaneychyk