I want to use useEffect to check if user signed in print username on menu. but I get this warning:
React Hook useEffect has an unnecessary dependency: 'authService._user'. Either exclude it or remove the dependency array. Outer scope values like 'authService._user' aren't valid dependencies because mutating them doesn't re-render the component react-hooks/exhaustive-deps
this is my useEffect in navbar:
useEffect(() => {
setUsername(authService._user.username);
}, [authService._user])
and this is my authorise service example:
interface User {
username: string;
password: string;
}
export class AuthorizeService {
_user: User = {
username: '',
password: ''
};
_isAuthenticated = false;
isAuthenticated() {
return this._isAuthenticated;
}
Authenticate(username: string, password: string) {
this._isAuthenticated = true; //I will add fetch latter here
this._user = {
username: username,
password: password
}
}
}
const authService = new AuthorizeService();
export default authService;
btw everything work great but there is warning in console.
my question is: is that the right way to do it? and why there is warning.