react-pair
v4.0.19
Published
๐๏ธ Util to help with the paired hook pattern
Maintainers
Readme
๐๏ธ Util to help with the paired hook pattern.
Usage
๐ฆ Node
Install react-pair as a dependency:
pnpm add react-pair
# or
npm install react-pair
# or
yarn add react-pairImport it and use it:
import { useState } from "react";
import { pair } from "react-pair";
const useCount = initialCount => {
const [count, setCount] = useState(initialCount);
return { onClick: () => setCount(count + 1), children: count };
};
const PairedCount = pair(useCount);
const Component = ({ array = [] }) => (
<ul>
{array.map(key => (
<PairedCount key={key}>
{usePairedCount => {
const props = usePairedCount(key);
return (
<li>
<button type="button" {...props} />
</li>
);
}}
</PairedCount>
))}
</ul>
);๐ฆ Deno
Import react-pair using the npm: prefix, and use it directly:
import { useState } from "npm:react";
import { pair } from "npm:react-pair";
const useCount = initialCount => {
const [count, setCount] = useState(initialCount);
return { onClick: () => setCount(count + 1), children: count };
};
const PairedCount = pair(useCount);
const Component = ({ array = [] }) => (
<ul>
{array.map(key => (
<PairedCount key={key}>
{usePairedCount => {
const props = usePairedCount(key);
return (
<li>
<button type="button" {...props} />
</li>
);
}}
</PairedCount>
))}
</ul>
);๐ Browser
Import react-pair using esm.sh, and use it directly:
<script type="module">
import { createElement, useState } from "https://esm.sh/react";
import { pair } from "https://esm.sh/react-pair";
const useCount = initialCount => {
const [count, setCount] = useState(initialCount);
return { onClick: () => setCount(count + 1), children: count };
};
const PairedCount = pair(useCount);
const Component = ({ array = [] }) =>
createElement("ul", {
children: array.map(key =>
createElement(PairedCount, {
key,
children: usePairedCount => {
const props = usePairedCount(key);
return createElement("li", {
children: createElement("button", props),
});
},
}),
),
});
</script>Useful links
- ๐ Documentation: TypeDoc generated documentation.
- โณ Changelog: List of changes between versions.
- โ Tests Coverage: Coveralls page with tests coverage.
