0
0
NextJSframework~20 mins

State and hooks in client components in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js State & Hooks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
state_output
intermediate
2:00remaining
What is the output after clicking the button twice?
Consider this Next.js client component using React hooks. What will be the displayed count after clicking the button two times?
NextJS
'use client';

import React, { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <button onClick={handleClick} aria-label="Increment count">
      Count: {count}
    </button>
  );
}
ACount: 1
BCount: 2
CCount: 0
DCount: 3
Attempts:
2 left
💡 Hint
Remember that state updates in React are asynchronous and may be batched.
component_behavior
intermediate
2:00remaining
Which option correctly updates state based on previous value?
In a Next.js client component, which option correctly increments the count twice when the button is clicked?
NextJS
'use client';

import React, { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    // Which option below correctly increments count twice?
  }

  return (
    <button onClick={handleClick} aria-label="Increment count">
      Count: {count}
    </button>
  );
}
A
setCount(prev =&gt; prev + 1);
setCount(prev =&gt; prev + 1);
B
setCount(() =&gt; count + 1);
setCount(() =&gt; count + 1);
C
setCount(count++);
setCount(count++);
D
setCount(count + 1);
setCount(count + 1);
Attempts:
2 left
💡 Hint
Use the functional form of setState to get the latest value.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in a Next.js client component?
Identify the option that will cause a syntax error when used in a Next.js client component with React hooks.
NextJS
'use client';

import React, { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState(0);

  return <div>{value}</div>;
}
Aconst [value, setValue] = useState(0);
Bconst [value, setValue] = useState;
Cconst value, setValue = useState(0);
D;)0(etatSesu = ]eulaVtes ,eulav[ tsnoc
Attempts:
2 left
💡 Hint
Check the correct syntax for array destructuring assignment.
🔧 Debug
advanced
2:00remaining
Why does this Next.js client component not update the displayed count?
This component does not update the count on button click. What is the cause?
NextJS
'use client';

import React, { useState } from 'react';

export default function Counter() {
  let count = 0;

  function handleClick() {
    count += 1;
  }

  return (
    <button onClick={handleClick} aria-label="Increment count">
      Count: {count}
    </button>
  );
}
AThe component is missing 'use client' directive.
BThe button's onClick handler is not correctly bound to the function.
CThe count variable is declared with let instead of const.
DState is not used; count is a local variable and React does not re-render on its change.
Attempts:
2 left
💡 Hint
React only re-renders when state or props change.
🧠 Conceptual
expert
2:00remaining
What happens if you call a React hook conditionally inside a Next.js client component?
Consider this code snippet inside a Next.js client component. What is the result of calling useState inside a conditional block?
NextJS
'use client';

import React, { useState } from 'react';

export default function ConditionalHook({ flag }) {
  if (flag) {
    const [count, setCount] = useState(0);
    return <div>Count: {count}</div>;
  } else {
    return <div>No count</div>;
  }
}
AIt works fine and updates state only when flag is true.
BIt causes a React hook rules violation and may break the component rendering.
CIt throws a syntax error because hooks cannot be inside if statements.
DIt causes the component to render twice on every update.
Attempts:
2 left
💡 Hint
React hooks must be called unconditionally in the same order on every render.