Error: Uncaught TypeError in AuthContext in Next.js 13.0.6 when using TypeScript and Firebase integration

I'm currently trying to display a page only if the user is present in my app. Right now, the app is pretty bare bones with just an AuthContext and this one page. I had it working in React, but ran into some issues when I switched it over to TS and Next13 - since I'm new to both technologies, any help would be greatly appreciated:

AuthContext.tsx:

'use client';
import { useContext, createContext, useEffect, useState } from 'react';
import {
  GoogleAuthProvider,
  signInWithPopup,
  signInWithRedirect,
  signOut,
  onAuthStateChanged,
  signInWithEmailAndPassword,
  User,
} from 'firebase/auth';
import { auth } from '../../../firebaseConfig';
import getUserData from '../api/getUserData';

//-----------------------interfaces-----------------------------------------

interface Props {
  children: React.ReactNode;
}

interface SignInCredentials {
  email: string;
  password: string;
}

export interface AuthContextValues {
  googleSignIn: () => void;
  emailSignIn: ({ email, password }: SignInCredentials) => void;
  logout: () => void;
  user: User | null;
  userProfileData: any;
  loadingUser: boolean;
}

...

page.tsx:

import React from 'react';
import { useUserAuth } from './../../components/shared/context/AuthContext';

type Props = {};

export default function UserPage(props: Props) {
  const { user } = useUserAuth();
  return <>{user?.uid}</>;
}

The main layout.tsx

import './global.css';
import { AuthContextProvider } from './../components/shared/context/AuthContext';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang='en'>
      <body>
        <AuthContextProvider>{children}</AuthContextProvider>
      </body>
    </html>
  );
}

and this is the error I am getting:

client.js?234c:691 Uncaught TypeError: (0 , _components_shared_context_AuthContext__WEBPACK_IMPORTED_MODULE_2__.useUserAuth) is not a function
    at UserPage (webpack-internal:///(sc_server)/./app/user/page.tsx:13:106)
    ...
  • I tried renaming the useUserAuth method but it didn't change the outcome.
  • This entire code worked without TypeScript in React18.

Answer №1

From what I gather, it seems like the solution involves inserting 'use client'; into your page.tsx

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

Error: The module 'https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js' is missing the required export 'default'

I'm currently in the process of setting up and testing Google authentication for a web application that I'm developing. Unfortunately, I've encountered several issues with getting the JavaScript functions to work properly, and I am uncertain ...

Display a fixed three levels of highchart Sunburst upon each click in Angular8

Looking to create a dynamic sunburst highchart that displays three levels at a time while allowing interactive drilling. For instance, if there are 5 levels, the chart should initially show the first three levels. When clicking on level 3, levels 2, 3, and ...

Having trouble launching Next.js on a Kubernetes cluster

I recently developed a Next.js application and successfully deployed it to a Kubernetes (AKS) cluster. The Docker file I used closely resembles the sample Next.js Docker files available here. The pod manifest is fairly standard, with environment variables ...

How can a particular route parameter in Vue3 with Typescript be used to retrieve an array of strings?

Encountered a build error: src/views/IndividualProgramView.vue:18:63 - error TS2345: Argument of type 'string | string[]' is not assignable to parameter of type 'string'. Type 'string[]' is not assignable to type 'strin ...

Join the Observable and formControl in Angular 4 by subscribing

My goal is to display the data retrieved from FireStore in the screen fields upon loading. However, the buildForm() function is being called before subscribing to the data, resulting in the failure to populate the screen fields with the FireStore data. pe ...

Navigating to a new page in NextJs using route.push() in a

I'm currently learning Next.js and practicing, but I've run into an issue. I'm hoping to find someone who can help me diagnose the problem. Below is my route.push() function: onClick={(e) => { const query = { oid: data.oid, ...

The refresh function in the table is not working as expected when implemented in a functional component. The table being used is Material

I am currently utilizing MaterialTable from https://material-table.com/#/docs/features/editable to manage data and perform CRUD operations within my application. I am seeking a way to automatically refresh the table data after any CRUD operation (add, upda ...

The Next.js page is functioning properly when run on localhost, but facing issues when deployed on

I've recently developed a blog for a website and just finished working on the search results page. Surprisingly, everything was functioning perfectly when tested on localhost. However, upon deployment to Vercel, the search functionality stopped workin ...

What situations call for the use of 'import * as' in TypeScript?

Attempting to construct a cognitive framework for understanding the functionality of import * as Blah. Take, for instance: import * as StackTrace from 'stacktrace-js'; How does this operation function and in what scenarios should we utilize imp ...

Tips for accessing values from a dynamically generated functional component in next.js

My component count is variable and changes dynamically. I add function components based on the count and then need to retrieve the values of their variables. const MainComponent: React.FC<Props> = ({ fields, label, addCount }) => { const co ...

Transform Observable RxJS into practical results

In a straightforward scenario, I have an upload action on the page that looks like this: onUpload$: Subject<SomeUpload> = new Subject<SomeUpload>(); uploadAction$: Observable<Action> = this.onUpload$.map(entity => this.someActionServi ...

Struggling with the @typescript-eslint/no-var-requires error when trying to include @axe-core/react? Here's a step-by

I have integrated axe-core/react into my project by: npm install --save-dev @axe-core/react Now, to make it work, I included the following code snippet in my index.tsx file: if (process.env.NODE_ENV !== 'production') { const axe = require(&a ...

Ensure that TypeScript compiled files are set to read-only mode

There is a suggestion on GitHub to implement a feature in tsc that would mark compiled files as readonly. However, it has been deemed not feasible and will not be pursued. As someone who tends to accidentally modify compiled files instead of the source fil ...

The CldUploadWidget creates an overlay featuring a never-ending loading spinner

Attempting to utilize the CldUploadWidget from the next-cloudinary package in a Next.js app with the app router. The goal is to upload an image and retrieve the public_id and URL, but upon setup, clicking the button that opens the overlay only displays a l ...

The functionality of getStaticProps is not operational when being used within the components

Following a recent issue I encountered, the question arises: typeError: Cannot read property 'map' of undefined. I attempted to utilize a component with getStaticProps in my pages/index.js, but alas, it doesn't function properly in conjunct ...

In the realm of Next.js 13.4 API streaming, tokens make their way to localhost in a staggered fashion, whereas in a production environment

Issue Encountered with API Streaming Behavior in Next.js 13.4 An unexpected issue has arisen with the API streaming functionality in Next.js 13.4 when using the app router, specifically related to the behavior of tokens being sent all at once instead of i ...

Resolving TypeScript errors when using the Mongoose $push method

It appears that a recent package upgrade involving mongoose or @types/mongoose is now triggering new typescript errors related to mongoose $push, $pull, $addToSet, and $each operators. For instance: await User.findByIdAndUpdate(request.user._id, { $ ...

Generating an array of keys from duplicated values in Typescript

My data is structured in the following array format: { itemTitle: 'value example', itemType: 'value example', itemDescription: 'value example', itemFamily: 'Asset', }, { itemTitle: 'val ...

The discord.js TypeScript is throwing an error stating that the 'index.ts' file is missing when trying to run 'ts-node index.ts'

I have been working on creating a discord bot using discord.js and TypeScript. However, when I attempt to start the bot by running 'ts-node index.ts', I encounter the following error: Error: Cannot find module 'node:events' Require stac ...

What is the meaning of boolean true in a Firestore query using TypeScript?

Currently, I am facing an issue with querying Firestore in Angular 8 using AngularFire. While querying a string like module_version works perfectly fine as shown in the code snippet below, the problem arises when attempting to query a boolean field in Fire ...