Skip to content

Commit 05d4050

Browse files
author
Bob Fanger
committed
feat: Using React hooks in Svelte components
1 parent 548eb50 commit 05d4050

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

docs/architecture.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Architecture
22

3-
This document describes design desicions and implementation details of the preprocessor.
3+
This document describes designdecisions and implementation details of the preprocessor.
4+
5+
**Principles:**
6+
7+
- Compatibility first, Ease-of-use second and Performance last.
48

59
## Context
610

src/lib/Hook.svelte

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script lang="ts">
2+
import used from "./used";
3+
4+
export let use: () => any;
5+
export let out: any = undefined;
6+
7+
let ready = false;
8+
9+
function setValue(value: any) {
10+
out = value;
11+
ready = true;
12+
}
13+
14+
function Hook<T>(props: { use: () => T; setValue(val: T): void }) {
15+
const value = props.use();
16+
props.setValue(value);
17+
return null;
18+
}
19+
used(Hook);
20+
</script>
21+
22+
<react:Hook {use} {setValue} />
23+
{#if ready}
24+
<slot {out} />
25+
{/if}

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as useStore } from "./useStore.js";
22
export { default as reactify } from "./reactify.js";
33
export { default as sveltify } from "./sveltify.js";
4+
export { default as Hook } from "./Hook.svelte";

src/lib/used.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
3+
/**
4+
* Svelte/TypeScript is not (yet) able to detect usage of the <react:ComponentX> syntax.
5+
* This causes `X is declared but its value is never read. (ts:6133)` errors.
6+
*
7+
* This function can be used to suppress these errors.
8+
*
9+
* Usage:
10+
*
11+
* used(ComponentX, ComponentY);
12+
*/
13+
export default function used(...args: any[]) {}

src/routes/+page.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88

99
<li><a href="/context-svelte">getContext()</a></li>
1010
<li><a href="/context-react">useContext()</a></li>
11+
12+
<li><a href="/hooks">Using hooks</a></li>
1113
</ul>

src/routes/hooks/+page.svelte

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts">
2+
import { Hook } from "$lib";
3+
import { useState } from "react";
4+
5+
function useSomeState() {
6+
return useState("Initial value");
7+
}
8+
9+
function useMultipleHooks() {
10+
const [x] = useState(1);
11+
const [y] = useState(2);
12+
return { x, y };
13+
}
14+
</script>
15+
16+
<Hook use={useSomeState} let:out={[value, setValue]}>
17+
<div>{value}</div>
18+
<button
19+
on:click={() => {
20+
setValue("Changed");
21+
}}>Change</button
22+
>
23+
</Hook>
24+
<hr />
25+
<Hook use={useMultipleHooks} let:out={position}>
26+
x: {position.x}
27+
y: {position.y}
28+
</Hook>

0 commit comments

Comments
 (0)