0
0
NextJSframework~20 mins

Conditional routes in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Next.js route component render when user is not authenticated?

Consider this Next.js page component that conditionally renders content based on authentication status.

import { useState, useEffect } from 'react';

export default function Dashboard() {
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  useEffect(() => {
    // Simulate auth check
    setTimeout(() => setIsAuthenticated(false), 1000);
  }, []);

  if (!isAuthenticated) {
    return <h1>Please log in to access the dashboard.</h1>;
  }

  return <h1>Welcome to your dashboard!</h1>;
}
NextJS
import { useState, useEffect } from 'react';

export default function Dashboard() {
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  useEffect(() => {
    // Simulate auth check
    setTimeout(() => setIsAuthenticated(false), 1000);
  }, []);

  if (!isAuthenticated) {
    return <h1>Please log in to access the dashboard.</h1>;
  }

  return <h1>Welcome to your dashboard!</h1>;
}
AIt first renders nothing, then after 1 second renders 'Please log in to access the dashboard.'.
BIt renders 'Welcome to your dashboard!' immediately and stays that way.
CIt first renders 'Please log in to access the dashboard.', then after 1 second stays the same.
DIt renders 'Please log in to access the dashboard.' immediately and stays that way.
Attempts:
2 left
💡 Hint

Think about the initial state and what the useEffect does after 1 second.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements a conditional redirect in Next.js 14 App Router?

You want to redirect users to '/login' if they are not authenticated inside a server component using Next.js 14 App Router. Which code snippet is correct?

NextJS
import { redirect } from 'next/navigation';

export default function Page() {
  const isAuthenticated = false;

  // Conditional redirect here
}
A
if (!isAuthenticated) {
  redirect('/login');
}
return &lt;h1&gt;Dashboard&lt;/h1&gt;;
B
if (!isAuthenticated) {
  router.push('/login');
}
return &lt;h1&gt;Dashboard&lt;/h1&gt;;
C
if (!isAuthenticated) {
  return redirect('/login');
}
return &lt;h1&gt;Dashboard&lt;/h1&gt;;
D
if (!isAuthenticated) {
  window.location.href = '/login';
}
return &lt;h1&gt;Dashboard&lt;/h1&gt;;
Attempts:
2 left
💡 Hint

Remember that redirect is a function that causes a server-side redirect and does not return JSX.

🔧 Debug
advanced
2:00remaining
Why does this conditional route cause a hydration error in Next.js?

Examine this Next.js page component:

import { useState, useEffect } from 'react';

export default function Profile() {
  const [isLoggedIn, setIsLoggedIn] = useState(null);

  useEffect(() => {
    setIsLoggedIn(true);
  }, []);

  if (isLoggedIn === null) {
    return <div>Loading...</div>;
  }

  if (!isLoggedIn) {
    return <div>Please log in.</div>;
  }

  return <div>Welcome back!</div>;
}

Why might this cause a hydration mismatch error?

NextJS
import { useState, useEffect } from 'react';

export default function Profile() {
  const [isLoggedIn, setIsLoggedIn] = useState(null);

  useEffect(() => {
    setIsLoggedIn(true);
  }, []);

  if (isLoggedIn === null) {
    return <div>Loading...</div>;
  }

  if (!isLoggedIn) {
    return <div>Please log in.</div>;
  }

  return <div>Welcome back!</div>;
}
ABecause conditional rendering inside useEffect is not allowed.
BBecause useEffect runs on server causing state to change before hydration.
CBecause useState cannot be initialized with null in Next.js components.
DBecause the initial render on server is 'Loading...' but client immediately changes to 'Welcome back!', causing mismatch.
Attempts:
2 left
💡 Hint

Think about what the server renders vs what the client renders initially.

state_output
advanced
2:00remaining
What is the output of this Next.js client component with conditional route logic?

Given this component, what will be displayed after 2 seconds?

import { useState, useEffect } from 'react';

export default function Home() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    setTimeout(() => setUser({ name: 'Alice' }), 2000);
  }, []);

  if (!user) {
    return <h2>Redirecting to login...</h2>;
  }

  return <h2>Hello, {user.name}!</h2>;
}
NextJS
import { useState, useEffect } from 'react';

export default function Home() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    setTimeout(() => setUser({ name: 'Alice' }), 2000);
  }, []);

  if (!user) {
    return <h2>Redirecting to login...</h2>;
  }

  return <h2>Hello, {user.name}!</h2>;
}
AImmediately shows 'Hello, Alice!' and never changes.
BImmediately shows 'Redirecting to login...', then after 2 seconds shows 'Hello, Alice!'.
CImmediately shows nothing, then after 2 seconds shows 'Redirecting to login...'.
DImmediately shows 'Redirecting to login...' and stays that way.
Attempts:
2 left
💡 Hint

Consider the initial state and when setUser updates the state.

🧠 Conceptual
expert
2:00remaining
Which statement about conditional routing in Next.js 14 App Router is TRUE?

Choose the correct statement about how conditional routing works in Next.js 14 App Router.

AYou can use the <code>redirect()</code> function inside server components to perform immediate server-side redirects based on conditions.
BClient components can use <code>redirect()</code> to navigate users without page reloads.
CConditional routing must always be handled in middleware, not in components.
DNext.js 14 App Router does not support conditional rendering based on user authentication.
Attempts:
2 left
💡 Hint

Think about where redirect() can be used and what it does.