Skip to content

Commit 5a8f346

Browse files
committed
Add useRef
1 parent 6370448 commit 5a8f346

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script>
2+
</script>

src/routes/(hooks)/useRef/+page.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { PageLoad } from "./$types"
2+
3+
export const load = (async ({ params }) => {
4+
return {
5+
title: "useRef",
6+
react: (await import("./react.jsx?raw")).default,
7+
svelte: (await import("./svelte.svelte?raw")).default,
8+
}
9+
}) satisfies PageLoad
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React, { useRef } from 'react'
2+
3+
export default function TextInputWithFocusButton() {
4+
const inputEl = useRef(null)
5+
const handleClick = () => {
6+
inputEl.current.focus()
7+
}
8+
9+
return (
10+
<>
11+
<input ref={inputEl} />
12+
<button onClick={handleClick}>Focus the input</button>
13+
</>
14+
)
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
let inputEl
3+
4+
function handleClick() {
5+
inputEl.focus()
6+
}
7+
</script>
8+
9+
<input bind:this={inputEl} />
10+
<button on:click={handleClick}>Focus the input</button>

src/routes/+layout.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
let tile1Url = "/useState"
1111
let tile2Url = "/useEffect"
1212
let tile3Url = "/useMemo"
13+
let tile4Url = "/useRef"
1314
</script>
1415

1516
<!-- App Shell -->
@@ -70,6 +71,14 @@
7071
href={tile3Url}
7172
class={tile3Url === $page.url.pathname ? "bg-primary-500" : ""}><Icon icon="code" /></AppRailTile
7273
>
74+
<AppRailTile
75+
label="useRef"
76+
title="useRef"
77+
value={4}
78+
tag="a"
79+
href={tile4Url}
80+
class={tile4Url === $page.url.pathname ? "bg-primary-500" : ""}><Icon icon="code" /></AppRailTile
81+
>
7382
</svelte:fragment>
7483

7584
<svelte:fragment slot="trail">

0 commit comments

Comments
 (0)