Skip to content

React Pair Reference

Coverage License NPM Version Open Issues Size

πŸ–‡οΈ Util to help with the paired hook pattern.

Usage

πŸ“¦ Node

Install react-pair as a dependency:

Terminal window
1
pnpm add react-pair
2
# or
3
npm install react-pair
4
# or
5
yarn add react-pair

Import it and use it:

1
import { useState } from "react";
2
import { pair } from "react-pair";
3
4
const useCount = initialCount => {
5
const [count, setCount] = useState(initialCount);
6
7
return { onClick: () => setCount(count + 1), children: count };
8
};
9
10
const PairedCount = pair(useCount);
11
12
const 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:

1
import { useState } from "npm:react";
2
import { pair } from "npm:react-pair";
3
4
const useCount = initialCount => {
5
const [count, setCount] = useState(initialCount);
6
7
return { onClick: () => setCount(count + 1), children: count };
8
};
9
10
const PairedCount = pair(useCount);
11
12
const 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>

Internal

PairedComponentProperties

Ζ¬ PairedComponentProperties<Hook>: Object

Paired component properties (just children with the paired hook render function).

Type parameters

NameType
Hookextends Function

Type declaration

NameTypeDescription
childrenPairedRenderFunction<Hook>Children has to be a function, and the argument is the paired hook.

View source


PairedRenderFunction

Ζ¬ PairedRenderFunction<Hook>: Unary<Hook, ReturnType<typeof createElement>>

Function that receives the paired hook and must return a ReactElement.

Type parameters

NameType
Hookextends Function

View source

Other

pair

β–Έ pair<Hook>(hook): FunctionComponent<PairedComponentProperties<Hook>> & { displayName: `paired(${string})` }

Creates a component with a function children that has the given hook in context.

Type parameters

NameType
Hookextends Function

Parameters

NameTypeDescription
hookHookHook to be paired.

Returns

FunctionComponent<PairedComponentProperties<Hook>> & { displayName: `paired(${string})` }

Component that expects a function as children with the paired hook.

Example

1
const useCount = initialCount => {
2
const [count, setCount] = useState(initialCount);
3
4
return { onClick: () => setCount(count + 1), children: count };
5
};
6
7
const PairedCount = pair(useCount);
8
9
const 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
);

View source

Module: preact-pair

Internal

PairedComponentProperties

Ζ¬ PairedComponentProperties<Hook>: Object

Paired component properties (just children with the paired hook render function).

Type parameters
NameType
Hookextends Function
Type declaration
NameTypeDescription
childrenPairedRenderFunction<Hook>Children has to be a function, and the argument is the paired hook.

View source


PairedRenderFunction

Ζ¬ PairedRenderFunction<Hook>: Unary<Hook, ReturnType<typeof h>>

Function that receives the paired hook and must return a VNode.

Type parameters
NameType
Hookextends Function

View source

Other

pair

β–Έ pair<Hook>(hook): FunctionComponent<PairedComponentProperties<Hook>> & { displayName: `paired(${Hook[β€œname”]})` }

Creates a component with a function children that has the given hook in context.

Type parameters
NameType
Hookextends Function
Parameters
NameTypeDescription
hookHookHook to be paired.
Returns

FunctionComponent<PairedComponentProperties<Hook>> & { displayName: `paired(${Hook[β€œname”]})` }

Component that expects a function as children with the paired hook.

Example

1
const useCount = initialCount => {
2
const [count, setCount] = useState(initialCount);
3
4
return { onClick: () => setCount(count + 1), children: count };
5
};
6
7
const PairedCount = pair(useCount);
8
9
const 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
);

View source