React Pair Reference
🖇️ Util to help with the paired hook pattern.
Usage
📦 Node
Install react-pair
as a dependency:
1pnpm add react-pair2# or3npm install react-pair4# or5yarn add react-pair
Import it and use it:
1import { useState } from "react";2import { pair } from "react-pair";3
4const useCount = initialCount => {5 const [count, setCount] = useState(initialCount);6
7 return { onClick: () => setCount(count + 1), children: count };8};9
10const PairedCount = pair(useCount);11
12const Component = ({ array = [] }) => (13 <ul>14 {array.map(key => (15 <PairedCount key={key}>16 {usePairedCount => {17 const props = usePairedCount(key);18
19 return (20 <li>21 <button type="button" {...props} />22 </li>23 );24 }}25 </PairedCount>26 ))}27 </ul>28);
🦕 Deno
Import react-pair
using the npm:
prefix, and use it directly:
1import { useState } from "npm:react";2import { pair } from "npm:react-pair";3
4const useCount = initialCount => {5 const [count, setCount] = useState(initialCount);6
7 return { onClick: () => setCount(count + 1), children: count };8};9
10const PairedCount = pair(useCount);11
12const Component = ({ array = [] }) => (13 <ul>14 {array.map(key => (15 <PairedCount key={key}>16 {usePairedCount => {17 const props = usePairedCount(key);18
19 return (20 <li>21 <button type="button" {...props} />22 </li>23 );24 }}25 </PairedCount>26 ))}27 </ul>28);
🌎 Browser
Import react-pair
using esm.sh, and use it directly:
1<script type="module">2 import { createElement, useState } from "https://esm.sh/react";3 import { pair } from "https://esm.sh/react-pair";4
5 const useCount = initialCount => {6 const [count, setCount] = useState(initialCount);7
8 return { onClick: () => setCount(count + 1), children: count };9 };10
11 const PairedCount = pair(useCount);12
13 const Component = ({ array = [] }) =>14 createElement("ul", {15 children: array.map(key =>16 createElement(PairedCount, {17 key,18 children: usePairedCount => {19 const props = usePairedCount(key);20
21 return createElement("li", {22 children: createElement("button", props),23 });24 },25 }),26 ),27 });28</script>
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
Internal
PairedComponentProperties<Hook>
1type PairedComponentProperties<Hook>: object;
Paired component properties (just children with the paired hook render function).
Type parameters
Type parameter |
---|
Hook extends Function |
Type declaration
Member | Type | Description |
---|---|---|
children | PairedRenderFunction <Hook > | Children has to be a function, and the argument is the paired hook. |
PairedRenderFunction<Hook>
1type PairedRenderFunction<Hook>: Unary<Hook, ReturnType<typeof createElement>>;
Function that receives the paired hook and must return a ReactElement
.
Type parameters
Type parameter |
---|
Hook extends Function |
Other
pair()
1function pair<Hook>(2 hook: Hook,3): FunctionComponent<PairedComponentProperties<Hook>> & object;
Creates a component with a function children that has the given hook in context.
Type parameters
Type parameter |
---|
Hook extends Function |
Parameters
Parameter | Type | Description |
---|---|---|
hook | Hook | Hook to be paired. |
Returns
FunctionComponent
<PairedComponentProperties
<Hook
>>
& object
Component that expects a function as children with the paired hook.
Example
1const useCount = initialCount => {2 const [count, setCount] = useState(initialCount);3
4 return { onClick: () => setCount(count + 1), children: count };5};6
7const PairedCount = pair(useCount);8
9const Component = ({ array = [] }) => (10 <ul>11 {array.map(key => (12 <PairedCount key={key}>13 {usePairedCount => {14 const props = usePairedCount(key);15
16 return (17 <li>18 <button type="button" {...props} />19 </li>20 );21 }}22 </PairedCount>23 ))}24 </ul>25);
preact-pair
Internal
PairedComponentProperties<Hook>
1type PairedComponentProperties<Hook>: object;
Paired component properties (just children with the paired hook render function).
Type parameters
Type parameter |
---|
Hook extends Function |
Type declaration
Member | Type | Description |
---|---|---|
children | PairedRenderFunction <Hook > | Children has to be a function, and the argument is the paired hook. |
PairedRenderFunction<Hook>
1type PairedRenderFunction<Hook>: Unary<Hook, ReturnType<typeof h>>;
Function that receives the paired hook and must return a VNode
.
Type parameters
Type parameter |
---|
Hook extends Function |
Other
pair()
1function pair<Hook>(2 hook: Hook,3): FunctionComponent<PairedComponentProperties<Hook>> & object;
Creates a component with a function children that has the given hook in context.
Type parameters
Type parameter |
---|
Hook extends Function |
Parameters
Parameter | Type | Description |
---|---|---|
hook | Hook | Hook to be paired. |
Returns
FunctionComponent
<PairedComponentProperties
<Hook
>>
& object
Component that expects a function as children with the paired hook.
Example
1const useCount = initialCount => {2 const [count, setCount] = useState(initialCount);3
4 return { onClick: () => setCount(count + 1), children: count };5};6
7const PairedCount = pair(useCount);8
9const Component = ({ array = [] }) => (10 <ul>11 {array.map(key => (12 <PairedCount key={key}>13 {usePairedCount => {14 const props = usePairedCount(key);15
16 return (17 <li>18 <button type="button" {...props} />19 </li>20 );21 }}22 </PairedCount>23 ))}24 </ul>25);