Skip to content

Instantly share code, notes, and snippets.

@teddyzetterlund
Last active October 27, 2021 16:22
Show Gist options
  • Save teddyzetterlund/61da39ffe6d5661989fb8adac0c4062d to your computer and use it in GitHub Desktop.
Save teddyzetterlund/61da39ffe6d5661989fb8adac0c4062d to your computer and use it in GitHub Desktop.
Posting FormData to Firebase Realtime Database from React
const PostForm = (props) => {
const handleSubmit = (event) => {
event.preventDefault();
const formData = new FormData(event.target);
let updates = {};
for (const [path, value] of formData.entries()) {
updates[path] = value;
}
firebase.database().ref('posts/').set(updates);
}
return (
<form method="post" onSubmit={handleSubmit}>
<label>Title <input name="title" /></label>
<label>Slug <input name="slug" /></label>
<input type="submit" />
</form>
)
}
ReactDOM.render(<PostForm />, document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment