Skip to content

Commit 4ac58f5

Browse files
committed
Purge useEffect
1 parent 3841fd5 commit 4ac58f5

File tree

6 files changed

+27
-41
lines changed

6 files changed

+27
-41
lines changed

basics/next-app-router/src/app/burrito/page.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useState, useEffect } from 'react';
3+
import { useState } from 'react';
44
import { useAuth } from '@/contexts/AuthContext';
55
import { useRouter } from 'next/navigation';
66
import posthog from 'posthog-js';
@@ -10,13 +10,9 @@ export default function BurritoPage() {
1010
const router = useRouter();
1111
const [hasConsidered, setHasConsidered] = useState(false);
1212

13-
useEffect(() => {
14-
if (!user) {
15-
router.push('/');
16-
}
17-
}, [user, router]);
18-
13+
// Redirect to home if not logged in
1914
if (!user) {
15+
router.push('/');
2016
return null;
2117
}
2218

basics/next-app-router/src/app/profile/page.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use client';
22

3-
import { useEffect } from 'react';
43
import { useAuth } from '@/contexts/AuthContext';
54
import { useRouter } from 'next/navigation';
65
import posthog from 'posthog-js';
@@ -9,13 +8,9 @@ export default function ProfilePage() {
98
const { user } = useAuth();
109
const router = useRouter();
1110

12-
useEffect(() => {
13-
if (!user) {
14-
router.push('/');
15-
}
16-
}, [user, router]);
17-
11+
// Redirect to home if not logged in
1812
if (!user) {
13+
router.push('/');
1914
return null;
2015
}
2116

basics/next-app-router/src/contexts/AuthContext.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
3+
import { createContext, useContext, useState, ReactNode } from 'react';
44
import posthog from 'posthog-js';
55

66
interface User {
@@ -20,17 +20,19 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined);
2020
const users: Map<string, User> = new Map();
2121

2222
export function AuthProvider({ children }: { children: ReactNode }) {
23-
const [user, setUser] = useState<User | null>(null);
23+
// Use lazy initializer to read from localStorage only once on mount
24+
const [user, setUser] = useState<User | null>(() => {
25+
if (typeof window === 'undefined') return null;
2426

25-
useEffect(() => {
2627
const storedUsername = localStorage.getItem('currentUser');
2728
if (storedUsername) {
2829
const existingUser = users.get(storedUsername);
2930
if (existingUser) {
30-
setUser(existingUser);
31+
return existingUser;
3132
}
3233
}
33-
}, []);
34+
return null;
35+
});
3436

3537
const login = async (username: string, password: string): Promise<boolean> => {
3638
try {
@@ -42,13 +44,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
4244

4345
if (response.ok) {
4446
const { user: userData } = await response.json();
45-
47+
4648
let localUser = users.get(username);
4749
if (!localUser) {
48-
localUser = userData;
50+
localUser = userData as User;
4951
users.set(username, localUser);
5052
}
51-
53+
5254
setUser(localUser);
5355
localStorage.setItem('currentUser', username);
5456

basics/next-pages-router/src/contexts/AuthContext.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
1+
import { createContext, useContext, useState, ReactNode } from 'react';
22
import posthog from 'posthog-js';
33

44
interface User {
@@ -18,17 +18,19 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined);
1818
const users: Map<string, User> = new Map();
1919

2020
export function AuthProvider({ children }: { children: ReactNode }) {
21-
const [user, setUser] = useState<User | null>(null);
21+
// Use lazy initializer to read from localStorage only once on mount
22+
const [user, setUser] = useState<User | null>(() => {
23+
if (typeof window === 'undefined') return null;
2224

23-
useEffect(() => {
2425
const storedUsername = localStorage.getItem('currentUser');
2526
if (storedUsername) {
2627
const existingUser = users.get(storedUsername);
2728
if (existingUser) {
28-
setUser(existingUser);
29+
return existingUser;
2930
}
3031
}
31-
}, []);
32+
return null;
33+
});
3234

3335
const login = async (username: string, password: string): Promise<boolean> => {
3436
try {

basics/next-pages-router/src/pages/burrito.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState } from 'react';
22
import Head from 'next/head';
33
import { useRouter } from 'next/router';
44
import posthog from 'posthog-js';
@@ -10,13 +10,9 @@ export default function BurritoPage() {
1010
const router = useRouter();
1111
const [hasConsidered, setHasConsidered] = useState(false);
1212

13-
useEffect(() => {
14-
if (!user) {
15-
router.push('/');
16-
}
17-
}, [user, router]);
18-
13+
// Redirect to home if not logged in
1914
if (!user) {
15+
router.push('/');
2016
return null;
2117
}
2218

basics/next-pages-router/src/pages/profile.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useEffect } from 'react';
21
import Head from 'next/head';
32
import { useRouter } from 'next/router';
43
import posthog from 'posthog-js';
@@ -9,13 +8,9 @@ export default function ProfilePage() {
98
const { user } = useAuth();
109
const router = useRouter();
1110

12-
useEffect(() => {
13-
if (!user) {
14-
router.push('/');
15-
}
16-
}, [user, router]);
17-
11+
// Redirect to home if not logged in
1812
if (!user) {
13+
router.push('/');
1914
return null;
2015
}
2116

0 commit comments

Comments
 (0)