Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reset Uncontrolled Forms by Forcing React to Rebuild the DOM.
import React, { useState, createRef } from "react";
const Form = () => {
const emailRef = createRef<HTMLInputElement>()
return (
<div>
Email:
<input type="text" ref={emailRef}/>
<button
onClick={() => {
// This check is necessary because in theory we could hit this
// code even though React is still rebuilding the DOM.
if (emailRef.current) {
window.alert(`Your email is "${emailRef.current.value}"`)
}
}}
>
Go
</button>
</div>
);
};
export {
Form,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment