5.0.2 • Published 1 month ago

use-value-enhancer v5.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

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 only triggers re-rendering when new value emitted from val (base on val equal`` notObject.iscomparison from ReactuseState`).

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 (Object.is comparison from React useState).

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 (Object.is comparison from React useState).

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>
);
5.0.2

1 month ago

5.0.1

2 months ago

5.0.0

3 months ago

4.1.3

3 months ago

4.1.2

3 months ago

4.1.1

4 months ago

4.1.0

4 months ago

4.0.0

5 months ago

3.0.1

9 months ago

2.1.1

10 months ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.8

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

0.1.1

2 years ago

2.0.0

2 years ago

0.1.0

2 years ago