JSPM

use-computed-state

1.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q37183F
  • License MIT

returns a value derived from other state values.

Package Exports

  • use-computed-state
  • use-computed-state/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-computed-state) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.