0
0
NextJSframework~20 mins

Conditional styling patterns in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Styling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered background color of the button?

Consider this Next.js functional component using conditional styling with Tailwind CSS:

import React, { useState } from 'react';

export default function ColorButton() {
  const [active, setActive] = useState(false);
  return (
    <button
      className={`px-4 py-2 font-bold text-white rounded ${active ? 'bg-green-500' : 'bg-red-500'}`}
      onClick={() => setActive(!active)}
      aria-pressed={active}
    >
      {active ? 'Active' : 'Inactive'}
    </button>
  );
}

What background color will the button have when first rendered?

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

export default function ColorButton() {
  const [active, setActive] = useState(false);
  return (
    <button
      className={`px-4 py-2 font-bold text-white rounded ${active ? 'bg-green-500' : 'bg-red-500'}`}
      onClick={() => setActive(!active)}
      aria-pressed={active}
    >
      {active ? 'Active' : 'Inactive'}
    </button>
  );
}
AThe button has no background color.
BThe button has a green background (bg-green-500).
CThe button has a red background (bg-red-500).
DThe button background color toggles rapidly on render.
Attempts:
2 left
💡 Hint

Look at the initial state value of active and how it affects the className.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies conditional inline styles in Next.js?

Given this Next.js component snippet, which option correctly applies a blue text color only when isBlue is true?

function Text({ isBlue }) {
  return (
    <p style={/* your code here */}>Hello</p>
  );
}
NextJS
function Text({ isBlue }) {
  return (
    <p style={/* your code here */}>Hello</p>
  );
}
A{ color: isBlue || 'blue' }
B{ color: isBlue && 'blue' }
C{ color: isBlue ? 'blue' : 'false' }
D{ color: isBlue ? 'blue' : undefined }
Attempts:
2 left
💡 Hint

Remember that inline style values must be strings or valid CSS values. Using false or undefined affects rendering differently.

🔧 Debug
advanced
2:00remaining
Why does this conditional className not update on state change?

Examine this Next.js component:

import { useState } from 'react';

export default function Toggle() {
  const [on, setOn] = useState(false);

  function toggle() {
    on = !on;
  }

  return (
    <button className={on ? 'bg-blue-500' : 'bg-gray-500'} onClick={toggle}>
      {on ? 'On' : 'Off'}
    </button>
  );
}

Why does clicking the button not change its background color?

NextJS
import { useState } from 'react';

export default function Toggle() {
  const [on, setOn] = useState(false);

  function toggle() {
    on = !on;
  }

  return (
    <button className={on ? 'bg-blue-500' : 'bg-gray-500'} onClick={toggle}>
      {on ? 'On' : 'Off'}
    </button>
  );
}
ADirectly assigning to 'on' does not update React state; use setOn instead.
BThe initial state is true, so the color never changes.
CThe toggle function is never called because onClick is misspelled.
DThe className syntax is invalid and causes a runtime error.
Attempts:
2 left
💡 Hint

Think about how React state variables should be updated.

🧠 Conceptual
advanced
2:00remaining
What is the advantage of using clsx or classnames for conditional styling?

In Next.js projects, libraries like clsx or classnames are often used for conditional class names. What is the main advantage of using them?

AThey allow combining multiple class names conditionally in a clean, readable way.
BThey automatically optimize CSS for faster loading.
CThey enable inline styles to be written as JavaScript objects.
DThey replace the need for CSS frameworks like Tailwind CSS.
Attempts:
2 left
💡 Hint

Think about how you manage multiple classes that depend on conditions.

state_output
expert
3:00remaining
What is the final rendered text and color after these state updates?

Consider this Next.js component:

import { useState, useEffect } from 'react';

export default function Status() {
  const [status, setStatus] = useState('idle');

  useEffect(() => {
    if (status === 'idle') {
      setStatus('loading');
    } else if (status === 'loading') {
      setTimeout(() => setStatus('done'), 100);
    }
  }, [status]);

  return (
    <div className={
      status === 'idle' ? 'text-gray-500' :
      status === 'loading' ? 'text-yellow-500' :
      'text-green-500'
    }>
      {status}
    </div>
  );
}

After the component mounts and all state updates finish, what text and text color class will be rendered?

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

export default function Status() {
  const [status, setStatus] = useState('idle');

  useEffect(() => {
    if (status === 'idle') {
      setStatus('loading');
    } else if (status === 'loading') {
      setTimeout(() => setStatus('done'), 100);
    }
  }, [status]);

  return (
    <div className={
      status === 'idle' ? 'text-gray-500' :
      status === 'loading' ? 'text-yellow-500' :
      'text-green-500'
    }>
      {status}
    </div>
  );
}
AText: 'idle' with class 'text-gray-500'.
BText: 'done' with class 'text-green-500'.
CText: 'loading' with class 'text-yellow-500'.
DText: 'done' with class 'text-yellow-500'.
Attempts:
2 left
💡 Hint

Consider how useEffect triggers on state changes and the timing of setTimeout.