1

I'm pretty new at typescript and migrating my react app to using ts.

I'm using the firebase auth service and I'm facing some type issues which I can't seem to get around. Please advise!

import React, { useEffect, useState } from 'react';
import app from './firebase';

export const AuthContext = React.createContext(null);

export const AuthProvider: React.FC<Props> = ({ children }) => {
  const [currentUser, setCurrentUser] = useState(null);
  const [pending, setPending] = useState(true);

  useEffect(() => {
    app.auth().onAuthStateChanged((user) => {
      // ts error here
      setCurrentUser(user);
      setPending(false);
    });
  }, []);

  if(pending){
    return <>Loading...</>;
  }

  return (
    <AuthContext.Provider
      // ts error here
      value={{ currentUser }}
    >
      {children}
    </AuthContext.Provider>
  );
};

interface Props {
  children: React.ReactNode;
}

setCurrentUser(user); is giving me an error of Argument of type 'User | null' is not assignable to parameter of type 'SetStateAction<null>'.

and

value={{ currentUser }} is giving me an error of Type '{ currentUser: null; }' is not assignable to type 'null'.

2 Answers 2

1

The user parameter that returns from firebase ,it's type is not assignable to null,so you should create a type or interface for that use or you could just try to add 'any' like this:

const [currentUser, setCurrentUser] = useState<any>(null);
Sign up to request clarification or add additional context in comments.

Comments

0

For me I added to the context and it worked

export const AuthContext = createContext<any>(null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.