Challenge - 5 Problems
React vs Traditional JavaScript Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this React component display?
Consider this React functional component using useState. What will be shown on the screen after clicking the button twice?
React
import React, { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </> ); }
Attempts:
2 left
💡 Hint
Remember that useState updates the state and re-renders the component.
✗ Incorrect
Each click increases the count by 1. After two clicks, count becomes 2 and is displayed.
🧠 Conceptual
intermediate2:00remaining
How does React differ from traditional JavaScript DOM manipulation?
Which statement best describes the main difference between React and traditional JavaScript when updating the user interface?
Attempts:
2 left
💡 Hint
Think about how React optimizes UI updates.
✗ Incorrect
React creates a virtual copy of the DOM and compares it to the previous version to update only changed parts, improving performance.
📝 Syntax
advanced2:00remaining
Which React code snippet correctly updates state based on previous state?
In React, what is the correct way to update state when the new value depends on the previous state?
Attempts:
2 left
💡 Hint
Use the updater function form of setState to avoid stale state.
✗ Incorrect
Using a function inside setCount ensures the update uses the latest state value, avoiding bugs when updates are asynchronous.
🔧 Debug
advanced2:00remaining
Why does this React component not update the displayed count?
Look at this React component. Why does clicking the button not change the displayed count?
React
import React, { useState } from 'react'; export default function Counter() { let count = 0; return ( <> <p>Count: {count}</p> <button onClick={() => { count += 1; }}>Increment</button> </> ); }
Attempts:
2 left
💡 Hint
React only re-renders when state or props change.
✗ Incorrect
Local variables like count do not trigger React to re-render. State must be used to update UI.
❓ lifecycle
expert2:00remaining
What happens when a React component with useEffect dependency array is rendered multiple times?
Given this React component, how many times will the effect run if the component re-renders multiple times without changing the 'value' prop?
React
import React, { useEffect } from 'react'; export default function Example({ value }) { useEffect(() => { console.log('Effect ran'); }, [value]); return <p>{value}</p>; }
Attempts:
2 left
💡 Hint
Think about how useEffect dependencies control when the effect runs.
✗ Incorrect
The effect runs only when the 'value' prop changes. Re-renders without 'value' change do not trigger the effect.