Socket
Socket
Sign inDemoInstall

use-computed-state

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    use-computed-state

returns a value derived from other state values.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
6.02 kB
Created
Weekly downloads
 

Readme

Source

use-computed-state

React hook that recomputes a value every time one of its dependencies change -- also works with promises/async functions

NPM

Install

yarn add use-computed-state

or if you use npm

npm install --save use-computed-state

Usage

import React, { useState } from 'react'
import useComputedState from 'use-computed-state'

const Example = () => {
  let [items, setItems] = useState([])
  let count = useComputedState(
    () => items.length,
    [items]
  )
  let promisedCount = useComputedState(
    async () => {
      let response = await Promise.resolve(items.length)
      return response
    },
    [items]
  )

  return (
    <div>
      <button onClick={addItem}>add item</button>
      <div>There are {count} items and {promisedCount} promised items</div>
    </div>
  )

  function addItem(ev) {
    ev.preventDefault()
    setItems([...items, {name: 'new item'}])
  }
}

License

Public Domain.

Keywords

FAQs

Last updated on 04 Jun 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