Socket
Socket
Sign inDemoInstall

use-awaitable-component

Package Overview
Dependencies
4
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    use-awaitable-component

React hook for awaiting component callback.


Version published
Weekly downloads
789
increased by0.51%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

use-awaitable-component

React hook for awaiting component callback.

Installation

npm

npm install use-awaitable-component

yarn

yarn add use-awaitable-component

Usage

import { useState } from "react";
import useAwaitableComponent from "use-awaitable-component";
import "./styles.css";

function Modal({ visible, onSubmit, onCancel }) {
  const [text, setText] = useState("");
  const display = visible ? "block" : "none";

  const handleSubmit = () => {
    onSubmit(text);
    setText("");
  };

  const handleCancel = () => {
    onCancel(":)");
  };

  return (
    <div className="modal" style={{ display }}>
      <div className="modal-head">This is a modal</div>
      <div className="modal-body">
        <input
          placeholder="Type something here..."
          value={text}
          onChange={(e) => setText(e.target.value)}
        />
        <button onClick={handleSubmit}>Submit</button>
      </div>
      <button className="modal-close" onClick={handleCancel}>
        X
      </button>
    </div>
  );
}

export default function App() {
  const [status, execute, resolve, reject, reset] = useAwaitableComponent();
  const showModal = status === "awaiting";

  const handleAwaitModal = async () => {
    try {
      const value = await execute();
      alert(`VALUE: ${value}`);
    } catch (err) {
      alert(`Canceled: ${err}`);
    } finally {
      reset();
    }
  };
  return (
    <div className="App">
      <div className="button-container">
        <button disabled={showModal} onClick={handleAwaitModal}>
          {showModal ? "Waiting..." : "Show Modal"}
        </button>
      </div>
      <Modal visible={showModal} onSubmit={resolve} onCancel={reject} />
    </div>
  );
}

Live Demo

See live demo on Codesandbox.

Reference

const [status, execute, resolve, reject, reset] = useAwaitableComponent()
FieldTypeDescriptions
status'idle' | 'awaiting' | 'resolved' | 'rejected'Current awaitable component status
execute() => PromiseA function to start execution
resolve(data: any) => voidA callback to call after component completed without error
reject(reason: any) => voidA callback to call after component completed with error
reset() => voidA function to reset status back to idle. This function must be called before calling execute() again.

Keywords

FAQs

Last updated on 29 Jan 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc