2

Im tyring to pass through Provider value ={ ..} the states and functions that i want react to listen on changes. I have login function that accept userId and token and a logout function that accept nothing. In the AuthContext i made an interface that declare the types that the functions accept but still i get an TS2322 error.

the Error is:

TS2322: Type '{ userId: string | null; token: string | null; isLoggedIn: boolean; login: (token: string, uid: string) => void; logout: () => void; }' is not assignable to type 'Auth'.   Object literal may only specify known properties, and 'login' does not exist in type 'Auth'.

AuthContext.tsx

import {createContext} from "react";

interface Auth{
  userId:string | null;
  token: string | null;
  isLoggedIn: boolean;
  (t:string | null, id:string | null) : void;
  (): void;
}

export const AuthContext = createContext<Auth | null>(null);

App.tsx

import {AuthContext} from "./shared/context/AuthContext";

const App: React.FC = () => {

const [token, setToken] = useState<string | null>(null);
const [userId, setUserId] = useState<string | null>(null);

const login = useCallback((token:string, uid:string) => {
    setToken(token);
    setUserId(uid);
}, []);

const logout = useCallback(() => {
    setToken(null);
    setUserId(null)
}, []);

return (
    <AuthContext.Provider value={{
        userId: userId,
        token: token,
        isLoggedIn: !!token,
        login: login, //  **** TS2322 Error at this line ****
        logout: logout
    }}>
        <div className="App">
            <BrowserRouter forceRefresh={true}>
                <Switch>
                    <Route exact path="/">
                        <Home/>
                    </Route>
                    <Route path="/login">
                        <LoginForm/>
                    </Route>
                    <Route path="/signup">
                        <RegisterForm/>
                    </Route>
                    <Route path="/cart">
                        <Cart/>
                    </Route>
                    <Redirect to="/" from="*"/>
                </Switch>
            </BrowserRouter>
        </div>
    </AuthContext.Provider>
);
}

export default App;

1 Answer 1

0

The error message is actually pretty clear. You need to define the login/logout properties specifically on the Auth interface.

interface Auth{
  userId:string | null;
  token: string | null;
  isLoggedIn: boolean;
  login(token:string | null, uid:string | null) : void;
  logout(): void;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Im pretty new in this so i thought i dont need to write the name of the function in Auth. It fixed it, thanks :)
No worries, I tried to make you realize that most of the time the answer is already given in the error message. Glad it fixed your problem!

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.