DEV Community

Discussion on: Context API with TypeScript and Next.JS

Collapse
 
krivoo3 profile image
OCTANE 💀

How do you pass properties to the logout methods?

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

As we are using typescript first set the type of arguments


type authContextType = {
    user: boolean;
    login: () => void;
    logout: (someParameter: any) => void;
};

Enter fullscreen mode Exit fullscreen mode

then pass make use of your properties


const logout = (someParameter: any) => {
        // use of your parameters
        setUser(false);
};

Enter fullscreen mode Exit fullscreen mode

And finally pass it to the function, where you are using it.

...
<div>
    <button onClick={login}>Login</button>
    <button onClick={() => logout(someProperty)}>Logout</button>
</div>
...
Enter fullscreen mode Exit fullscreen mode