Exciting release!Introducing "safe npm". Learn more
Socket
Log inDemoInstall

use-delayed-state

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Issues
File Explorer

Advanced tools

use-delayed-state

React hook for setting State with delay

    1.2.0latest
    GitHub

Version published
Maintainers
1
Weekly downloads
593
increased by38.23%

Weekly downloads

Readme

Source

use-delayed-state

React hook for setting State with delay

NPM JavaScript Style Guide

This is an enhanced useState hook which accepts delay for setState as an extra argument. In most basic form it works same as React useState hook.

Use cases

It can be used for debouncing which simply delays all consecuative attempts for setting a state and if the last call persist for enough time the actual setState runs.

It is also a handy tool for applying timing logics inside react components e.g. Showing a notification for few seconds.

Install

npm install --save use-delayed-state

Usage

import React from 'react' import useDelayedState from 'use-delayed-state' export default function myComponent() { const [state, setState] = useDelayedState( 'Hello, I will be change in 5 seconds' ) setState('I am new State', 5000) return <div>{state}</div> }

In above example State will be updated after 5 second.

Debouncing example is more advance implementation of this hook which the source code is available here.

Usage details

const [state, setState, cancelSetState] = useDelayedState(initialState); // works like regular useState hook // it will not cancel any ongoing delayed setStates setState(newState); // or setState(newState, 0) // setState with delay // duration unit is millisecond // it will cancel any ongoing delayed setStates setState(newState, duration); // canceling any ongoing delayed state cancelSetState(); // setState along with setState with delay in one render // below codes set State to newState and it will set State to futureState after 2s setState(newState); setState(futureState, 2000);

License

MIT © makannew

Keywords

FAQs

Last updated on 10 Feb 2021

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket
support@socket.devSocket SOC 2 Logo

Product

  • Package Issues
  • 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