|
| 1 | +<img alt="Coven Engineering Pair logo" src="https://raw.githubusercontent.com/covenengineering/libraries/main/@coven/pair/logo.svg" height="108" /> |
| 2 | + |
| 3 | +[](https://jsr.io/@coven/pair) |
| 4 | +[](https://jsr.io/@coven/pair/score) |
| 5 | + |
| 6 | +🧩 [Paired hook pattern](https://lou.cx/articles/the-paired-hook-pattern) |
| 7 | +helper. |
| 8 | + |
| 9 | +## Examples |
| 10 | + |
| 11 | +### Preact |
| 12 | + |
| 13 | +```tsx |
| 14 | +import { useState } from "preact"; |
| 15 | +import { pair } from "@coven/pair/preact"; |
| 16 | + |
| 17 | +const useCount = (initialCount) => { |
| 18 | + const [count, setCount] = useState(initialCount); |
| 19 | + |
| 20 | + return { onClick: () => setCount(count + 1), children: count }; |
| 21 | +}; |
| 22 | + |
| 23 | +const PairedCount = pair(useCount); |
| 24 | + |
| 25 | +const Component = ({ array = [] }) => ( |
| 26 | + <ul> |
| 27 | + {array.map((key) => ( |
| 28 | + <PairedCount key={key}> |
| 29 | + {(usePairedCount) => { |
| 30 | + const props = usePairedCount(key); |
| 31 | + |
| 32 | + return ( |
| 33 | + <li> |
| 34 | + <button type="button" {...props} /> |
| 35 | + </li> |
| 36 | + ); |
| 37 | + }} |
| 38 | + </PairedCount> |
| 39 | + ))} |
| 40 | + </ul> |
| 41 | +); |
| 42 | +``` |
| 43 | + |
| 44 | +### React |
| 45 | + |
| 46 | +```tsx |
| 47 | +import { useState } from "react"; |
| 48 | +import { pair } from "@coven/pair/react"; |
| 49 | + |
| 50 | +const useCount = (initialCount) => { |
| 51 | + const [count, setCount] = useState(initialCount); |
| 52 | + |
| 53 | + return { onClick: () => setCount(count + 1), children: count }; |
| 54 | +}; |
| 55 | + |
| 56 | +const PairedCount = pair(useCount); |
| 57 | + |
| 58 | +const Component = ({ array = [] }) => ( |
| 59 | + <ul> |
| 60 | + {array.map((key) => ( |
| 61 | + <PairedCount key={key}> |
| 62 | + {(usePairedCount) => { |
| 63 | + const props = usePairedCount(key); |
| 64 | + |
| 65 | + return ( |
| 66 | + <li> |
| 67 | + <button type="button" {...props} /> |
| 68 | + </li> |
| 69 | + ); |
| 70 | + }} |
| 71 | + </PairedCount> |
| 72 | + ))} |
| 73 | + </ul> |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +## Other links |
| 78 | + |
| 79 | +- [Coverage](https://coveralls.io/github/covenengineering/libraries). |
0 commit comments