Skip to content

Instantly share code, notes, and snippets.

@rbrtsmith
Created July 31, 2016 14:50
Show Gist options
  • Save rbrtsmith/c9ad843e7cef3d17677f3fd8c8aefac9 to your computer and use it in GitHub Desktop.
Save rbrtsmith/c9ad843e7cef3d17677f3fd8c8aefac9 to your computer and use it in GitHub Desktop.
Despite common opinion it is possible to use refs inside of a functional React component...
const CommentForm = () => {
let author, text;
const submitForm = e => {
e.preventDefault();
console.log(author.value);
console.log(text.value);
};
return (
<form onSubmit={submitForm}>
<input type="text" ref={node => author = node} placeholder="Name" />
<input type="text" ref={node => text = node} placeholder="Comment" />
<button type="submit">
submit
</button>
</form>
);
};
@rbrtsmith
Copy link
Author

This totally works, as proven by the console.logs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment