Kwasi Afriyie
Kwasi Afriyie

Reputation: 1

how to save user form input data to firestore in reactnative

i've been trying to submit user input to my firebase database but it doesn't work for me. i'm a beginner in react-native and firebase. i have 3 input fields and want to save the data entered by user into my database after clicking on submit.

Upvotes: 0

Views: 2024

Answers (2)

FPROG
FPROG

Reputation: 21

Let's say you have some data in the state from the text input:

const [dataState,setDataState] = useState("Some data from text Input")

Now let's say you want to upload it to firebasee. First you need to have the firebase app initialized. Often you do this in a separate file. Here's how:

import  {initializeApp} from 'firebase/app'
import {firestore} from 'firebase/firestore'

const firebaseConfig = {
    ..your keys 
}

initializeApp(firebaseConfig)
const db = firestore()

export db;

Now that you have your app initialized, go to your main file/screen (like app.js):

import {db} from './yourFirebaseFile'
import  {collection,addDoc} from 'firebase/firestore'

Now let's say you have the data in your state from the text input. Let's create a function to send the data to firebase (you can all this function on press of a button)

const sendToFirestore = async() => {
      await addDoc( collection(db, "Your_Collections_Name_in_firebase"), {
               dataFromInput : dataState
  } )}

There you go! You have the data saved (:

Upvotes: 2

Dimitri Enjelvin
Dimitri Enjelvin

Reputation: 105

Let's say you have those inputs ready, let's say you have them stored in i don't know states maybe. Then if we talk about firestore you would do something like this :

const onSubmit = () => {

firestore() 
.collection('your collection') 
.doc(auth().currentUser.uid)
.set({
name : state.name, 
email : state.email
// and so on 
}) 
.then(() => {
// do something like logging 'user registered' 
}) 
} 

Note that the comments you received earlier are rights. This is just an example of what you can do, guessing this is what you were asking. But your question need to be more precise in the future so you can get better support

Also check the firebase or react-native-firebase documentations depending on which one you are using because the way of doing things might be a bit different. The one i showed you is with the react-native-firebase library

Upvotes: 1

Related Questions