useRef
React 341 characters
import React, { useRef } from 'react'
export default function TextInputWithFocusButton() {
const inputEl = useRef(null)
const handleClick = () => {
inputEl.current.focus()
}
return (
<>
<input ref={inputEl} />
<button onClick={handleClick}>Focus the input</button>
</>
)
}
Svelte 165 characters (52% less)
<script>
let inputEl
function handleClick() {
inputEl.focus()
}
</script>
<input bind:this={inputEl} />
<button on:click={handleClick}>Focus the input</button>