Search code examples
javascriptfirebasereact-nativegoogle-cloud-firestorereact-props

How to add different <text> to firestore?


I am trying to send some text to the firestore doc and it sends no issues.

Please refer to part of a code of the picture: https://ibb.co/dL0V0yv

However i have a couple of buttons and i want to set my code in a such way so that every onPress will send different text. I could achieve that by re-writing the generateDoc fucntion with diffenet state manually, but i wonder how could i do it with copiying? Do something with props of function so that the text could be change inside onPress, but i do not knwo how to produce that. Please help.


Solution

  • Give the function a parameter, like this...

    generateDoc = taskName => {
      return firestore().collection('Rooms')... // OP code here
        .set({ Task: taskName }).then...
    }
    

    and call it like this...

    onPress={this.generateDoc('task name for this button')}
    

    Edit

    To call two or more functions onPress, it's probably better to keep the template code clean and combine two promise returning functions into one, like this...

    operationA = param => {
      return firestore()...   // be sure to return the promise
    }
    
    operationB = param => {
      return firestore()...   // same thing here, return the promise
    }
    
    operationAandB = (paramA, paramB) => {
      // if the A and B are independent, then do them together
      return Promise.all([ operationA(paramA), operationB(paramB) ])
    
      // or if A and B must run in sequence, then
      return operationA(paramA).then(() => operationB(paramB))
    }
    
    onPress={this.operationAandB('paramA', 'paramB')}