Last active
October 27, 2021 16:22
-
-
Save teddyzetterlund/61da39ffe6d5661989fb8adac0c4062d to your computer and use it in GitHub Desktop.
Posting FormData to Firebase Realtime Database from React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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