What is the main difference between React and traditional JavaScript when building user interfaces?
React uses components to build UI in a structured and reusable way, while traditional JavaScript manipulates the DOM directly and often mixes HTML, CSS, and JS in less organized ways.
Click to reveal answer
intermediate
How does React improve updating the user interface compared to traditional JavaScript?
React uses a virtual DOM to efficiently update only the parts of the UI that change, avoiding slow full page or large DOM updates common in traditional JavaScript.
Click to reveal answer
beginner
What role do components play in React compared to functions in traditional JavaScript?
Components in React are like building blocks that manage their own content and state, making UI easier to build and maintain, unlike traditional JS functions which often just run code without UI structure.
Click to reveal answer
beginner
Why is JSX important in React compared to traditional JavaScript?
JSX lets you write HTML-like code inside JavaScript, making UI code clearer and easier to understand, while traditional JavaScript requires manual DOM creation and manipulation.
Click to reveal answer
intermediate
How does React handle state differently than traditional JavaScript?
React uses hooks like useState to keep track of data that changes over time and automatically updates the UI, whereas traditional JavaScript requires manual DOM updates when data changes.
Click to reveal answer
What does React use to efficiently update the UI?
ACSS animations
BDirect DOM manipulation
CjQuery selectors
DVirtual DOM
✗ Incorrect
React uses a virtual DOM to update only the parts of the UI that change, improving performance.
In React, what is JSX used for?
AWriting HTML-like code inside JavaScript
BStyling components
CFetching data from APIs
DManaging browser cookies
✗ Incorrect
JSX allows writing HTML-like syntax directly in JavaScript, making UI code easier to read and write.
Which of these is a key feature of React components?
AThey run server-side only
BThey manage their own state and UI
CThey replace HTML tags in the browser
DThey only handle CSS styling
✗ Incorrect
React components manage their own state and UI, making them reusable and organized.
How does traditional JavaScript usually update the UI?
ABy directly changing the DOM elements
BUsing virtual DOM
CWith React hooks
DThrough JSX syntax
✗ Incorrect
Traditional JavaScript updates the UI by directly manipulating DOM elements.
What React hook is commonly used to handle changing data in components?
AuseEffect
BuseContext
CuseState
DuseRef
✗ Incorrect
useState is the React hook used to manage state and update the UI when data changes.
Explain how React's approach to building user interfaces differs from traditional JavaScript.
Think about how React organizes UI and updates it compared to manual DOM changes.
You got /4 concepts.
Describe the benefits of using React components over traditional JavaScript functions for UI development.
Consider how components help keep UI code organized and manageable.
You got /4 concepts.
Practice
(1/5)
1. What is a key difference between React and traditional JavaScript when building web pages?
easy
A. React requires writing HTML only, traditional JavaScript uses CSS only.
B. React uses components and JSX, while traditional JavaScript manipulates the DOM directly.
C. React cannot update the page dynamically, traditional JavaScript can.
D. React uses server-side rendering only, traditional JavaScript is client-side only.
Solution
Step 1: Understand React's approach
React builds UI using components and JSX, which is a syntax that looks like HTML inside JavaScript.
Step 2: Understand traditional JavaScript approach
Traditional JavaScript changes the page by directly manipulating the DOM elements, which can be more complex and error-prone.
Final Answer:
React uses components and JSX, while traditional JavaScript manipulates the DOM directly. -> Option B
Quick Check:
React uses components and JSX = A [OK]
Hint: React uses components; traditional JS changes DOM directly [OK]
Common Mistakes:
Thinking React only uses HTML or CSS
Believing React cannot update pages dynamically
Confusing server-side and client-side rendering
2. Which of the following is the correct way to create a React functional component?
easy
A. class MyComponent extends React.Component { render() { return 'Hello'; } }
B. var MyComponent =
Hello
;
C. function MyComponent() { return
Hello
; }
D. function MyComponent() { document.write('Hello'); }
A React functional component is a function returning JSX, like function MyComponent() { return <div>Hello</div>; }.
Step 2: Check other options for correctness
var MyComponent =
Hello; assigns JSX directly to a variable, not a function. class MyComponent extends React.Component { render() { return 'Hello'; } } is a class component, not functional. function MyComponent() { document.write('Hello'); } uses document.write, which is not React.
Final Answer:
function MyComponent() { return <div>Hello</div>; } -> Option C
Quick Check:
Functional component returns JSX function = A [OK]
Hint: Functional components are functions returning JSX [OK]
Common Mistakes:
Confusing class components with functional components
Using document.write inside React components
Assigning JSX to variables without functions
3. What will be the output on the page when this React component is rendered?
function Greeting() {
const name = 'Alice';
return <h1>Hello, {name}!</h1>;
}
medium
A. Hello, Alice!
B. Hello, name!
C. <h1>Hello, {name}!</h1>
D. Error: name is not defined
Solution
Step 1: Understand JSX expression evaluation
Inside JSX, curly braces {name} insert the value of the variable name, which is 'Alice'.
Step 2: Determine rendered output
The component returns <h1>Hello, Alice!</h1>, so the page shows the text 'Hello, Alice!'.
Final Answer:
Hello, Alice! -> Option A
Quick Check:
JSX inserts variable value = C [OK]
Hint: JSX {variable} shows variable value, not text {variable} [OK]
Common Mistakes:
Thinking JSX shows {name} literally
Confusing variable name with string output
Expecting an error due to variable usage
4. Identify the error in this React component code:
function Counter() {
let count = 0;
function increment() {
count++;
}
return <button onClick={increment}>Count: {count}</button>;
}
medium
A. Using let instead of const for count
B. onClick should be written as onclick
C. Missing semicolon after count++
D. The increment function does not update the displayed count
Solution
Step 1: Understand React state behavior
React components must use state (like useState) to update UI dynamically. Here, count is a normal variable and changes won't re-render the component.
Step 2: Identify why increment doesn't update UI
Increment changes count variable but React does not know to re-render, so displayed count stays 0.
Final Answer:
The increment function does not update the displayed count -> Option D
Quick Check:
State needed to update UI = D [OK]
Hint: Use React state (useState) to update UI, not plain variables [OK]
Common Mistakes:
Thinking let vs const affects UI update
Ignoring React's re-render on state change
Using wrong event attribute case
5. You want to build a dynamic list of items that updates when a user adds a new item. Which approach best uses React instead of traditional JavaScript?
hard
A. Use React's useState hook to store the list and update it, then render the list in JSX.
B. Use document.getElementById to find the list element and append new items manually.
C. Write a for loop to create HTML strings and set innerHTML of the list container.
D. Use React class components with componentDidMount to update the list directly.
Solution
Step 1: Understand React's dynamic UI approach
React uses state (like useState) to hold data and re-renders UI automatically when state changes.
Step 2: Compare with traditional DOM manipulation
Traditional JavaScript manipulates DOM directly (options A and C), which is harder to manage and less efficient.
Step 3: Consider React class components
Use React class components with componentDidMount to update the list directly. uses class components and lifecycle methods, but modern React prefers functional components with hooks.
Final Answer:
Use React's useState hook to store the list and update it, then render the list in JSX. -> Option A
Quick Check:
React state and JSX rendering = B [OK]
Hint: Use useState hook for dynamic lists in React [OK]
Common Mistakes:
Using direct DOM methods inside React components
Relying on innerHTML string building
Using outdated class components instead of hooks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.