How do I call firebase functions from within a react component

Thomas Fox

I'm trying to work out how to call firebase functions from within a react component.

React component...

import React from 'react';
import './App.css';
import functions from '../functions/index'

function App() {

    const callFirebaseFunction = event =>{
        var output = functions.returnMessage()
        console.log(output)
    }

    return(
        <div>
            <button onClick={event => callFirebaseFunction()}>call function button</button>
        </div>    
    )

firebase functions index.js...

const functions = require('firebase-functions');

exports.returnMessage = functions.https.onCall((data, context) => {
    return {
        output: "the firebase function has been run"
      }
});

Hopefully my code explains what i'm trying to do. Feel free to correct any other mistakes that i'm making in this very simple example.

The issue that i'm having is that I can't import anything to the component that isn't within the src folder and the firebase functions falls outside of this. I don't really want to eject anything as i'm assuming that there is a proper way for me to access my firebase functions and I simply can't find it.

robsiemb

Per the documentation you need to use the firebase library in the client side to make the call to the callable function. You shouldn't be importing the server-side functions code, that needs to be deployed to firebase functions on its own -- it is not reused in the client side of the app.

Assuming you have properly imported the firebase client SDK into your client code, this means we can modify callFirebaseFunction like so:

const callFirebaseFunction = event => {
    const callableReturnMessage = firebase.functions().httpsCallable('returnMessage');

    callableReturnMessage().then((result) => {
      console.log(result.data.output);
    }).catch((error) => {
      console.log(`error: ${JSON.stringify(error)}`);
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do i call web service from within a web service

How do I call a function from within a nested function in typescript?

How do I get the parent context from within a component in glimmer?

ReactJS - how do I call a parent method from a child component?

How do I call a method from another class component in React.js

How do I call setState from another Component in ReactJs

How do I call a function from within a statement in the event listener?

How do I pass object from array within one component?

How can I call a function from one component to another? React

how do i call a component in react native from a native-android widget?

How do I call a mixin function from another component?

How do I call CreateMenu() from within an Apps Script Library?

How do I export or call method from component in another component in react?

How do I add the ability to edit text within a react component?

How do I insert a component within a MAP of other component in React?

How would I call a function from another component in react native?

React: How do I access a component from another component?

How do I iterate over a list of functions and call those functions within the loop

How do I call a function from Service to Component in Angular?

How do I call a function from another component

How do I call my function from within my function?

how do you call a method from another react class component

How can I call a function in a component from other components in React?

How to call existing Cloud functions from React Firebase app

How do I initialize firebase within a Vue3 component?

How do I call functions in another child from the child in react native when imported?

How can I call Child function from Parent Component in React

How do I grab the value of an object within a functional component in React and set it within useState?

How do I check if a document exists within a collection (Firebase REACT)?