React: Importing functions from other files is not allowed

As someone coming from an Angular background, I recently started using React and everything was going smoothly until I encountered an error while checking if Firebase is initialized:

TypeError: _assets_Services__Firebase__WEBPACK_IMPORTED_MODULE_11__.default.isInitialised is not a function

Let's take a look at the relevant part of my App.js:

import Firebase from './assets/Services/Firebase';

function App() {
  const [isLoggedIn, setLogin] = useState(true)
  const [firebaseInitialized, setFirebaseInitialized] = useState(true)

  useEffect(() => {
    Firebase.isInitialised().then(val => {
      setFirebaseInitialized(val)
    })
  })

And here is a snippet from my Firebase.js file:

class Firebase {
      constructor() {
        app.initializeApp(firebaseConfig);
        this.auth = app.auth();
        this.db = app.firestore();
      }

      isInitialized() {
          return new Promise(resolve => {
              this.auth.onAuthStateChanged(resolve)
          })
      }

After some research, I discovered that version mismatch might be causing the issue. Here's a snippet from my package.json:

"react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.1",

Answer №1

To access the methods of the Firebase class, you first need to instantiate an object of that class.

const firebase = new Firebase();
firebase.isInitialized()

If you define isInitialised as static, you can call it in the following way:

static isInitialized() {
  return new Promise(resolve => {
    this.auth.onAuthStateChanged(resolve)
  })
}

You would then use the method like this:

Firebase.isInitialized()

Update

It seems there was a typo in your code. You are calling it with isInitialised, when it should be isInitialized.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging data schemas to manage the feedback from APIs

I am curious about the benefits of modeling the API response on the client side. Specifically: First scenario: const [formData, setFormData] = useState(null); ... useEffect(() => { const callback = async () => { try { const fetchDa ...

Tips for effectively implementing a custom theme alongside a component library that utilizes Material UI without the need to apply it to every single component

What is the correct way to utilize a custom theme with a component lib that utilizes Material UI without wrapping every single component? import { createMuiTheme } from '@material-ui/core/styles'; const theme = createMuiTheme({ palette: { ...

A guide on automatically focusing on a Material UI Formik form TextField using React and TypeScript

I'm attempting to automatically focus my textField, but the 'autoFocus' attribute only seems to work after I submit the form and add a value. If no values are added (i.e. when opening the miui modal for the first time), the autoFocus does no ...

When you click on the text, the calendar will pop up for you to

When the text "SetExpiryDate" is clicked, a date picker opens. After selecting a date, the text changes to "Expires ${date}" where the selected date is inserted. If no date is selected, the text remains as it is. I'm curious how I can implement this ...

Material Design UI: Position the dialog in the bottom left corner of the screen

I recently developed a project to oversee employee activities, which includes a sidebar feature. In the initial stages of the project development, I noticed that there was a sidebar with a button inside it. When I clicked on the button, a dialog box popped ...

Error Encountered While Uploading to Firebase Functions: HTTP Error: 400, Mysterious Issue Un

Whilst attempting to deploy the server side of my Angular Universal SSR app to Firebase Functions, I encountered an issue with the error message Upload Error: HTTP Error: 400, Unknown Error. My understanding is that this error commonly occurs when deployi ...

issue with useReducer not functioning as expected

I encountered an issue with my customized Select component. It includes a select and options while listening for onChange events. Additionally, I am using a useReducer function that initializes some variables. However, even after selecting an option, the s ...

Error in React/Redux: props are undefined when using mapDispatchToProps

I am currently searching for information within a database using a react app. Within the actions.js file, I can see the response.data data when I utilize console.table. However, even after exporting, when attempting to display the information, the variabl ...

Tips for incorporating attributes into a customized Material-UI props component using TypeScript in React

I'm interested in using material-ui with react and typescript. I want to pass properties to the components, but I'm having trouble figuring out how to do it. Currently, I'm working with the react-typescript example from the material-UI repos ...

I'm curious if it's possible to superimpose a png image and specific coordinates onto a map by utilizing react-map

I am attempting to showcase a png graphic on a react-map-gl map, following the approach outlined here. Unfortunately, the image is not appearing as expected and no error messages are being generated for me to troubleshoot. Below is the snippet of code I&a ...

Troubleshooting in Vercel: Unable to render PNG file in Next.js

While working on a project using Next.js and Sass, I encountered an issue when deploying it on Vercel. The PNG images did not render properly, whereas the SVG files displayed without any problems. Interestingly, the PNG images render fine in my localhost ...

Having trouble displaying a set of data points in a React component

Exploring React, I have built a test app and performed an API call to fetch a large JSON object. After breaking it down into 10 arrays with 3 props each, I am now facing a challenge in sending this data to another component. Despite being able to log the ...

AgGrid Encounters Difficulty in Recovering Original Grid Information

After making an initial API call, I populate the grid with data. One of the fields that is editable is the Price cell. If I edit a Price cell and then click the Restore button, the original dataset is restored. However, if I edit a Price cell again, the ...

A step-by-step guide on integrating Firebase authentication with a Next.js application

I'm facing difficulties when trying to create a next.js app with express and firebase-authentication. I'm also unsure about the role of redux in this setup. Can anyone please assist me with this or recommend some tutorials? I've found that t ...

What is the best way to apply multiple array filters to an object list in react.js?

Looking to filter an array of items using multiple filter arrays in order to display only the items that match all selected filters. For example: The main array contains a table with the following data: ID TypeID LocationID Name 1 2 ...

Issue with prop inheritance in componentInheritance problem with passing props to

How can I pass the state function setMyChoice as a prop to the GamePlay component in a rock paper scissors Nextjs application? The goal is to produce a value for comparison. In the Gameplay component, I then pass a prop to another component called PlayButt ...

Is there a way to use a React-Bootstrap-Glyphicon to access the dropdown with React-Select?

I was looking to implement a dropdown using React-Select, but instead of the typical appearance as shown here: https://i.stack.imgur.com/UNSgr.png My goal was for it to have a look similar to this: https://i.stack.imgur.com/OfX40.png The current code I& ...

Material-UI CardMedia is experiencing difficulty in loading dynamic images from a URL, however, it is functioning properly with

I have created a function that retrieves image URLs based on the memType: const renderPicture = (memType, memID) => { if ((memType === "Music")) { toString(memID.music.map((albumArt) => (albumArt.albumArt))) } else if ((memT ...

Text overlay on Material UI Card

I am currently using the Material UI Card and CardMedia components in my application, but I am having trouble with overlaying text on top of the image. Below is a simplified version of what I have been attempting: <Card> <CardMedia image={this. ...

Windows authentication login only appears in Chrome after opening the developer tools

My current issue involves a React app that needs to authenticate against a windows auth server. To achieve this, I'm hitting an endpoint to fetch user details with the credentials included in the header. As per my understanding, this should trigger th ...