0
0
Reactframework~20 mins

Functional components in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Functional Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React functional component?
Consider this React functional component. What will it render on the screen?
React
import React, { useState } from 'react';

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default Counter;
A<div>Count: 1<button>Increment</button></div> initially, and count increases by 1 each click
BComponent throws an error because useState is used incorrectly
C<div>Count: 0<button>Increment</button></div> initially, and count increases by 1 each click
D<div>Count: 0<button>Increment</button></div> but clicking button does not change count
Attempts:
2 left
💡 Hint
Remember useState initializes the state value and updates it on button click.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a React functional component?
Select the option that correctly defines a React functional component named Greeting that returns a heading with 'Hello World'.
Aconst Greeting = () => <h1>Hello World</h1>;
Bconst Greeting = () => { <h1>Hello World</h1> }
Cfunction Greeting() { <h1>Hello World</h1> }
Dfunction Greeting() { return <h1>Hello World</h1>; }
Attempts:
2 left
💡 Hint
Arrow functions can return JSX directly without braces if only one expression.
lifecycle
advanced
2:00remaining
What happens when useEffect has an empty dependency array in a functional component?
In a React functional component, what is the behavior of useEffect when its dependency array is empty?
React
import React, { useEffect } from 'react';

function Example() {
  useEffect(() => {
    console.log('Effect ran');
  }, []);

  return <div>Check console</div>;
}

export default Example;
AThe effect runs only once after the first render
BThe effect runs after every render
CThe effect never runs
DThe effect runs only before the component unmounts
Attempts:
2 left
💡 Hint
Think about how dependency arrays control when effects run.
🔧 Debug
advanced
2:00remaining
Why does this functional component cause an error?
Identify the cause of the error in this React functional component code.
React
import React, { useState } from 'react';

function Toggle() {
  const [isOn, setIsOn] = useState(false)

  return (
    <button onClick={setIsOn(!isOn)}>
      {isOn ? 'ON' : 'OFF'}
    </button>
  );
}

export default Toggle;
AJSX syntax error due to missing parentheses
BThe onClick handler calls setIsOn immediately instead of on click
CuseState is missing an initial value
DThe component lacks a return statement
Attempts:
2 left
💡 Hint
Check how the onClick handler is assigned and when the function runs.
state_output
expert
2:00remaining
What is the final value of count after clicking the button twice quickly?
Given this React functional component, what will be the value of count after clicking the button twice quickly?
React
import React, { useState } from 'react';

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

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment Twice</button>
    </div>
  );
}

export default Counter;
ACount increases by 1, final value is 1
BComponent throws an error due to multiple setState calls
CCount remains 0, no change
DCount increases by 2, final value is 2
Attempts:
2 left
💡 Hint
Remember how React batches state updates and how setState with the same value behaves.