📖 Scenario: You are creating a small React app that shows a friendly greeting message on a webpage. This is like writing a simple note to welcome visitors.
🎯 Goal: Build a React functional component named Greeting that displays the text "Hello, welcome to React!" on the page.
📋 What You'll Learn
Create a functional component named Greeting
Use JSX to return a <div> containing the greeting text
Export the Greeting component as default
Use React 19+ functional component syntax with hooks (if needed)
💡 Why This Matters
🌍 Real World
React functional components are the building blocks of modern web apps. They let you create reusable pieces of UI that show information and respond to user actions.
💼 Career
Knowing how to write and compose functional components is essential for frontend developer roles working with React, a popular library used by many companies.
Progress0 / 4 steps
1
Set up the React component function
Create a functional component named Greeting using the syntax function Greeting() {}.
React
Hint
Start by writing a function named Greeting with empty curly braces.
2
Add JSX to return a div with greeting text
Inside the Greeting function, add a return statement that returns a <div> containing the text Hello, welcome to React!.
React
Hint
Use return <div>Your text</div>; inside the function.
3
Export the Greeting component as default
Add a line to export the Greeting component as the default export using export default Greeting;.
React
Hint
Write export default Greeting; after the function.
4
Use the Greeting component in an app
Create a new functional component named App that returns the <Greeting /> component inside a <main> tag. Export App as default instead of Greeting.
React
Hint
Write a new function App that returns <main><Greeting /></main> and export App as default.
Practice
(1/5)
1. What is a React functional component?
easy
A. A JavaScript function that returns JSX to render UI
B. A class that extends React.Component
C. A CSS style sheet for React components
D. A special HTML tag used in React
Solution
Step 1: Understand React component types
React components can be functions or classes that return UI elements.
Step 2: Identify functional component definition
A functional component is a plain JavaScript function returning JSX, not a class or style.
Final Answer:
A JavaScript function that returns JSX to render UI -> Option A
Quick Check:
Functional component = function returning JSX [OK]
Hint: Functional components are just functions returning JSX [OK]
Common Mistakes:
Confusing functional components with class components
Thinking functional components are CSS or HTML tags
Believing functional components must extend React.Component
2. Which of the following is the correct syntax for a React functional component named Greeting?
easy
A. const Greeting =
Hello!
;
B. function Greeting() { return
Hello!
; }
C. class Greeting extends React.Component { render() { return
Hello!
; } }
D. function Greeting { return
Hello!
; }
Solution
Step 1: Check function declaration syntax
function Greeting() { return
Hello!
; } correctly declares a function with parentheses and returns JSX.
Step 2: Identify syntax errors in other options
const Greeting =
Hello!
; assigns JSX directly without a function wrapper; class Greeting extends React.Component { render() { return
Hello!
; } } is a class component, not functional; function Greeting { return
Hello!
; } misses parentheses in function declaration.
Final Answer:
function Greeting() { return
Hello!
; } -> Option B
Quick Check:
Function syntax with parentheses and return JSX [OK]
Hint: Functional components need parentheses after function name [OK]
Common Mistakes:
Omitting parentheses in function declaration
Confusing class components with functional components
Assigning JSX directly without a function
3. What will be rendered by this React functional component?
useState is a React hook and must be imported explicitly from 'react'.
Step 2: Verify other code parts
The return statement and JSX syntax are correct; calling setCount in onClick is valid.
Final Answer:
useState is not imported from React -> Option C
Quick Check:
Hooks require import from React [OK]
Hint: Always import useState from React before using it [OK]
Common Mistakes:
Forgetting to import useState
Thinking setCount can't be called in event handlers
Assuming JSX syntax errors without checking imports
5. You want to create a functional component that shows a list of items passed as props and highlights the selected item on click. Which approach correctly uses hooks and props together?
hard
A. Use useState to track selected item and map props.items to render list with onClick handlers updating state
B. Use class component with this.state and this.props to manage selected item and render list
C. Use useEffect to directly modify props.items and highlight selected item
D. Render props.items without state and rely on CSS :hover to highlight selection
Solution
Step 1: Understand state and props roles
Props provide data (items), state tracks UI changes (selected item).
Step 2: Choose correct React hooks usage
useState is used to track selected item; mapping props.items with onClick updates state correctly.
Step 3: Evaluate other options
Class components are outdated here; useEffect should not modify props; CSS :hover can't track selection state.
Final Answer:
Use useState to track selected item and map props.items to render list with onClick handlers updating state -> Option A
Quick Check:
State for selection + props for data = Use useState to track selected item and map props.items to render list with onClick handlers updating state [OK]
Hint: Use useState for UI changes, props for data input [OK]
Common Mistakes:
Trying to modify props directly
Using class components instead of functional with hooks
Relying on CSS hover for persistent selection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.