0
0
Reactframework~20 mins

React vs traditional JavaScript - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React vs Traditional JavaScript Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
    </>
  );
}
ACount: 0
BCount: NaN
CCount: 1
DCount: 2
Attempts:
2 left
💡 Hint
Remember that useState updates the state and re-renders the component.
🧠 Conceptual
intermediate
2: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?
AReact uses a virtual DOM to efficiently update only parts of the real DOM that changed.
BReact updates the UI by changing the real DOM directly on every change.
CTraditional JavaScript uses a virtual DOM, but React manipulates the real DOM directly.
DReact requires manual DOM queries and updates like traditional JavaScript.
Attempts:
2 left
💡 Hint
Think about how React optimizes UI updates.
📝 Syntax
advanced
2: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?
AsetCount(prevCount => prevCount + 1);
BsetCount(count + 1);
CsetCount(() => count + 1);
DsetCount(count++);
Attempts:
2 left
💡 Hint
Use the updater function form of setState to avoid stale state.
🔧 Debug
advanced
2: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>
    </>
  );
}
ABecause the button's onClick handler is not called correctly.
BBecause count is a local variable and React does not track its changes for rendering.
CBecause useState is not initialized with count.
DBecause the component is missing a return statement.
Attempts:
2 left
💡 Hint
React only re-renders when state or props change.
lifecycle
expert
2: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>;
}
AThe effect runs only once when the component mounts.
BThe effect runs every time the component re-renders, regardless of 'value'.
CThe effect runs only when 'value' changes between renders.
DThe effect never runs because the dependency array is empty.
Attempts:
2 left
💡 Hint
Think about how useEffect dependencies control when the effect runs.