Skip to content

Commit dd819b1

Browse files
committed
docs: add example for using refs to access DOM values directly in referencing-values-with-refs.md
1 parent 6be2b02 commit dd819b1

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/content/learn/referencing-values-with-refs.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,39 @@ export default function Chat() {
655655

656656
</Sandpack>
657657

658+
If you don't need the input to be controlled (e.g., no live validation or display elsewhere), you can skip state entirely and read the DOM value directly via a ref.
659+
660+
<Sandpack>
661+
662+
```js
663+
import { useRef } from 'react';
664+
665+
export default function Chat() {
666+
const inputRef = useRef(null);
667+
668+
function handleSend() {
669+
setTimeout(() => {
670+
alert('Sending: ' + inputRef.current.value);
671+
}, 3000);
672+
}
673+
674+
return (
675+
<>
676+
<input
677+
ref={inputRef}
678+
/>
679+
<button
680+
onClick={handleSend}>
681+
Send
682+
</button>
683+
</>
684+
);
685+
}
686+
```
687+
688+
</Sandpack>
689+
690+
658691
</Solution>
659692

660693
</Challenges>

0 commit comments

Comments
 (0)