npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-value-enhancer

v5.0.6

Published

React hooks for value-enhancer.

Downloads

617

Readme

use-value-enhancer

Build Status npm-version Coverage Status minified-size

Commitizen friendly Conventional Commits code style: prettier

React hooks for value-enhancer.

Install

npm add use-value-enhancer value-enhancer react

Usage

useVal

useVal accepts a val from anywhere and returns the latest value.

It triggers re-rendering when new value emitted from val (base on val $version instead of React's Object.is comparison).

import { val } from "value-enhancer";
import { useVal } from "use-value-enhancer";

const val$ = val("value");

function Component({ val$ }) {
  const value = useVal(val$);
  return <p>{value}</p>;
}

useDerived

useDerived accepts a val from anywhere and returns the latest derived value.

Re-rendering is triggered when the derived value changes.

import { val } from "value-enhancer";
import { useDerived } from "use-value-enhancer";

const val$ = val("1");

function Component({ val$ }) {
  const derived = useDerived(val$, value => Number(value));
  return <p>{derived}</p>;
}

useFlatten

useFlatten accepts a val from anywhere and returns the latest value from the flatten val.

Re-rendering is triggered when the flatten value changes.

import { val } from "value-enhancer";
import { useFlatten } from "use-value-enhancer";

const val$ = val(val("1"));

function Component({ val$ }) {
  const value = useFlatten(val$); // "1"
  return <p>{value}</p>;
}
import { val } from "value-enhancer";
import { reactiveMap } from "value-enhancer/collections";
import { useFlatten } from "use-value-enhancer";

const map = reactiveMap();
map.set("a", val("1"));

function Component({ map }) {
  const value = useFlatten(map.$, map => map.get("a")); // "1"
  return <p>{value}</p>;
}

useKeys

useKeys accepts a reactive collection and returns the latest keys() as array.

import { reactiveMap } from "value-enhancer/collections";
import { useKeys } from "use-value-enhancer";

const map = reactiveMap();

function Component({ map }) {
  const keys = useKeys(map);
  return keys.map(key => <p key={key}>{key}</p>);
}

useValues

useValues accepts a reactive collection and returns the latest values() as array.

import { reactiveSet } from "value-enhancer/collections";
import { useValues } from "use-value-enhancer";

const set = reactiveSet();

function Component({ set }) {
  const values = useValues(set);
  return values.map(value => <p key={String(value)}>{value}</p>);
}

Example

import React, { createContext, useContext } from "react";
import ReactDOM from "react-dom/client";
import { val } from "value-enhancer";
import { useVal } from "use-value-enhancer";

const valFromProps$ = val("Props");
const valFromContext$ = val("Context");
const valFromExternal = val("External");

const ValContext = createContext(valFromContext$);

export const App = ({ valFromProps$ }) => {
  const valFromProps = useVal(valFromProps$);

  const valFromContext$ = useContext(ValContext);
  const valFromContext = useVal(valFromContext$);

  const valFromExternal = useVal(valFromExternal$);

  return (
    <>
      <p>
        {valFromProps}, {valFromContext}, {valFromExternal}
      </p>
      <p>Props, Context, External</p>
    </>
  );
};

ReactDOM.createRoot(document.getElementById("root")).render(
  <ValContext.Provider value={valFromContext$}>
    <App valFromProps$={valFromProps$} />
  </ValContext.Provider>
);