admin管理员组文章数量:1309971
I am having problem with a useEffect inside a ponent which is in every ponent. I have made some authentication and redirects in that ponent but I figured that when I use Nextjs link or the go back button from the browser, the Layout useEffect is not re rendering which means it is not checking if the user is logged in for example.
here's my layout ponent
const Layout = ({ children }) => {
const router = useRouter()
const { setAuthenticated, userFromDB, setUserFromDB, setIsVender, isVender } = useContext(AuthContext)
//cuando hacemos un login el token se guarda en el local storage asi que probamos que exista
useEffect(() => {
const checkToken = async () => {
const token = localStorage.getItem('FBIdToken')
if (token) {
const decodedToken = jwtDecode(token);
//si la fecha de expiracion del token supero a la actual, redirect al login
if (decodedToken.exp * 1000 < Date.now()) {
setAuthenticated(false)
router.replace('/login');
} else {
if(!userFromDB){
const user = await getUser(decodedToken.user_id);
//si encontro un usuario, lo asignamos al context api
if(user){
console.log(user)
// obtenemos el objecto con los datos del usuario y seteamos en el context si es vendedor o no
if(typeof user.isVender !== undefined){
user.isVender ? setIsVender(true) : setIsVender(false);
}
setUserFromDB(user);
}
setAuthenticated(true)
}
}
console.log('ejecutando');
} else {
if(router.pathname !== "/" &&
router.pathname !== "/download-app" &&
router.pathname !== "/register" &&
router.pathname !== "/login"
) {
console.log(router.pathname, 'redirigiendo')
router.replace('/login')
}
}
}
//probamos que haya un token y redirigimos al usuario en base a el
checkToken();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isVender])
return (
<div>
{children}
</div>
)
}
this is how I import it inside the _app.js ponent
return (
<AuthProvider>
<Layout>
<Component {...pageProps} />
</Layout>
</AuthProvider>
)
}
I hope you can help me, thanks in advance!
I am having problem with a useEffect inside a ponent which is in every ponent. I have made some authentication and redirects in that ponent but I figured that when I use Nextjs link or the go back button from the browser, the Layout useEffect is not re rendering which means it is not checking if the user is logged in for example.
here's my layout ponent
const Layout = ({ children }) => {
const router = useRouter()
const { setAuthenticated, userFromDB, setUserFromDB, setIsVender, isVender } = useContext(AuthContext)
//cuando hacemos un login el token se guarda en el local storage asi que probamos que exista
useEffect(() => {
const checkToken = async () => {
const token = localStorage.getItem('FBIdToken')
if (token) {
const decodedToken = jwtDecode(token);
//si la fecha de expiracion del token supero a la actual, redirect al login
if (decodedToken.exp * 1000 < Date.now()) {
setAuthenticated(false)
router.replace('/login');
} else {
if(!userFromDB){
const user = await getUser(decodedToken.user_id);
//si encontro un usuario, lo asignamos al context api
if(user){
console.log(user)
// obtenemos el objecto con los datos del usuario y seteamos en el context si es vendedor o no
if(typeof user.isVender !== undefined){
user.isVender ? setIsVender(true) : setIsVender(false);
}
setUserFromDB(user);
}
setAuthenticated(true)
}
}
console.log('ejecutando');
} else {
if(router.pathname !== "/" &&
router.pathname !== "/download-app" &&
router.pathname !== "/register" &&
router.pathname !== "/login"
) {
console.log(router.pathname, 'redirigiendo')
router.replace('/login')
}
}
}
//probamos que haya un token y redirigimos al usuario en base a el
checkToken();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isVender])
return (
<div>
{children}
</div>
)
}
this is how I import it inside the _app.js ponent
return (
<AuthProvider>
<Layout>
<Component {...pageProps} />
</Layout>
</AuthProvider>
)
}
I hope you can help me, thanks in advance!
Share Improve this question asked Jan 13, 2022 at 17:07 Joaquín VarelaJoaquín Varela 4096 silver badges22 bronze badges 2-
1
This:
// eslint-disable-next-line react-hooks/exhaustive-deps
is almost always an indication that there's a bug in the code. It's probably covering up the bug in your code. Don't do that unless you are sure that you are right and the lint rule is wrong. That effect will only re-run ifisVender
(whatever that is) changes. – Jared Smith Commented Jan 13, 2022 at 17:13 - I removed it and nothing happened, I think it was because of a warning but is gone now – Joaquín Varela Commented Jan 13, 2022 at 17:47
1 Answer
Reset to default 8If you want useEffect to trigger on router changes, you must add it as a dependancy of your useEffect hook:
useEffect(() => {
...
}, [isVender, router.pathname])
本文标签: javascriptNextjs UseEffect is not rendering on route changeStack Overflow
版权声明:本文标题:javascript - Nextjs UseEffect is not rendering on route change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741859846a2401558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论