Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
49
rated 0 times [  50] [ 1]  / answers: 1 / hits: 11603  / 2 Years ago, mon, october 31, 2022, 12:00:00

Now I'm building the application using React.js. All pages are working excepting of auth page. After logging successfully, it should bring the user to the home page but it was broken, and showed the blank page. After refreshing the manually, it started to show the home page.


When I checked the application thru development tools in the chrome browser, it says "Uncaught TypeError: destroy is not a function".
I attached the code where caused the error.


...
const UnauthedWrapper = () => {
const navigate = useNavigate();
const location = useLocation();
const {
state: { auth, user },
} = useContext(AppContext);

useEffect(() => {
if (auth && user && user.emailVerified && user.dstoreName) {
navigate(`/app/overview`);
return null;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [auth, user]);

return (
<>
{!location.pathname.includes("/auth") ? (
<Header
logo="/images/logo.png"
page="landing"
hideLogin={process.env.REACT_APP_ENV === "PROD"}
/>
) : (
<Link to="/">
<img
src="/images/logo.png"
alt="logo"
className="logo ms-4 mt-4"
width={180}
/>
</Link>
)}
...

More From » reactjs

 Answers
1

It turns out this almost always happens when you try to return anything from your useEffect hook that is not a function.



  • Why Doesn’t This Work?


If you return anything from a useEffect function, it must be a function.


useEffect(() => {
if (auth && user && user.emailVerified && user.dstoreName) {
navigate(`/app/overview`);
return null;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [auth, user]);


  • The Quick Solution



  1. Remove the unnecessary return.


useEffect(() => {
if (auth && user && user.emailVerified && user.dstoreName) {
navigate(`/app/overview`);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [auth, user]);


  1. Make sure it has function.


useEffect(() => {
if (auth && user && user.emailVerified && user.dstoreName) {
navigate(`/app/overview`);
return () => {}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [auth, user]);

[#18] Tuesday, August 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ethanc

Total Points: 57
Total Questions: 111
Total Answers: 111

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
ethanc questions
Thu, Jan 28, 21, 00:00, 3 Years ago
Fri, Dec 11, 20, 00:00, 4 Years ago
Mon, Jul 13, 20, 00:00, 4 Years ago
Tue, May 12, 20, 00:00, 4 Years ago
Sat, Jan 25, 20, 00:00, 4 Years ago
;