📖 Scenario: You are building a simple web page to explain why React is popular for creating user interfaces.
🎯 Goal: Create a React functional component that shows a list of reasons why React is used.
📋 What You'll Learn
Create a list of reasons as an array of strings
Create a variable to hold the number of reasons
Use a map function to render each reason inside a
element
Return a
element containing all the reasons
💡 Why This Matters
🌍 Real World
React is widely used to build interactive and fast web user interfaces by breaking UI into reusable components.
💼 Career
Understanding why React is used helps beginners appreciate component-based design and prepares them for frontend development roles.
Progress0 / 4 steps
1
Create the reasons array
Create a constant array called reasons with these exact strings: 'Reusable components', 'Fast rendering with virtual DOM', 'Strong community support'.
React
Hint
Use const reasons = [ ... ] with the exact strings inside.
2
Create a count variable
Create a constant called count that stores the length of the reasons array.
React
Hint
Use const count = reasons.length; to get the number of reasons.
3
Render the reasons list
Create a React functional component called WhyReact that returns a <ul> element. Inside the <ul>, use reasons.map with reason and index to create <li> elements for each reason. Use index as the key.
React
Hint
Use function WhyReact() { return ( ... ) } and inside return a <ul> with mapped <li> elements.
4
Export the component
Add export default WhyReact; at the end of the code to export the component.
React
Hint
Use export default WhyReact; to make the component usable elsewhere.
Practice
(1/5)
1. Why do developers use React for building websites?
easy
A. To design database schemas
B. To write backend server code easily
C. To manage network security settings
D. To create reusable components that make development faster
Solution
Step 1: Understand React's main feature
React breaks websites into small, reusable parts called components, which helps developers build faster and maintainable code.
Step 2: Compare options with React's purpose
Only To create reusable components that make development faster describes creating reusable components, which is React's core use. Other options relate to backend, database, or security, which React does not handle.
Final Answer:
To create reusable components that make development faster -> Option D
Quick Check:
Reusable components = C [OK]
Hint: React is mainly for building UI components fast [OK]
Common Mistakes:
Confusing React with backend frameworks
Thinking React manages databases
Assuming React handles security
2. Which of the following is the correct way to define a React functional component?
easy
A. component MyComponent() { return
Hello
; }
B. function MyComponent() { return
Hello
; }
C. def MyComponent() { return
Hello
; }
D. func MyComponent() { return
Hello
; }
Solution
Step 1: Recall React functional component syntax
React functional components are JavaScript functions that return JSX, defined using the keyword 'function'.
Step 2: Check each option's syntax
function MyComponent() { return
Hello; } uses 'function' correctly. Options B, C, and D use invalid keywords not recognized in JavaScript or React.
Final Answer:
function MyComponent() { return <div>Hello</div>; } -> Option B
Quick Check:
React components use 'function' keyword [OK]
Hint: React components start with 'function' keyword [OK]
Common Mistakes:
Using Python or other language keywords
Confusing component declaration syntax
Omitting return statement or JSX
3. What will be the result of this React component rendering?
function Greeting() {
const name = 'Alice';
return <h1>Hello, {name}!</h1>;
}
medium
A. Syntax Error
B. Hello, {name}!
C. <h1>Hello, Alice!</h1>
D. <h1>Hello, name!</h1>
Solution
Step 1: Understand JSX expression embedding
In JSX, curly braces { } embed JavaScript expressions. Here, {name} inserts the value of the variable 'name', which is 'Alice'.
Step 2: Predict rendered output
The component returns an h1 element with text 'Hello, Alice!'. So the rendered HTML is <h1>Hello, Alice!</h1>.
Final Answer:
<h1>Hello, Alice!</h1> -> Option C
Quick Check:
JSX {name} inserts variable value [OK]
Hint: JSX uses { } to insert variables [OK]
Common Mistakes:
Thinking {name} prints literally
Confusing JSX with plain HTML
Expecting syntax error for valid JSX
4. Identify the error in this React component code:
function Welcome() {
return (
<div>
<h1>Welcome</h1>
<p>Have a nice day!</p>
</div>
)
console.log('Rendered');
}
medium
A. console.log is unreachable after return statement
B. Missing semicolon after return
C. JSX tags are not closed properly
D. Function name should be lowercase
Solution
Step 1: Analyze function flow
The return statement ends the function execution, so any code after it is unreachable.
Step 2: Check the console.log placement
console.log('Rendered') is after return, so it will never run, which is a logical error.
Final Answer:
console.log is unreachable after return statement -> Option A
Quick Check:
Code after return is unreachable [OK]
Hint: Code after return won't run [OK]
Common Mistakes:
Ignoring unreachable code warnings
Thinking semicolon is mandatory after return
Assuming function names must be lowercase
5. How does React improve website speed when updating the user interface?
hard
A. By updating only the parts of the page that change using a virtual DOM
B. By reloading the entire page on every change
C. By using server-side rendering exclusively
D. By disabling user interactions during updates
Solution
Step 1: Understand React's update mechanism
React uses a virtual DOM to track changes and updates only the parts of the real DOM that need to change, avoiding full page reloads.
Step 2: Compare options with React's behavior
By updating only the parts of the page that change using a virtual DOM correctly describes this selective update process. Options B, C, and D describe incorrect or unrelated behaviors.
Final Answer:
By updating only the parts of the page that change using a virtual DOM -> Option A
Quick Check:
Virtual DOM updates only changed parts [OK]
Hint: React updates only changed parts for speed [OK]
Common Mistakes:
Thinking React reloads whole page
Confusing client and server rendering
Assuming React disables interactions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.