Functional components let you create parts of a webpage using simple JavaScript functions. They make building user interfaces easy and clear.
Functional components in React
function ComponentName(props) { return ( <div> {/* UI elements here */} </div> ); }
The function name should start with a capital letter.
It returns JSX, which looks like HTML but is JavaScript.
function Greeting() { return <h1>Hello, friend!</h1>; }
function Welcome(props) { return <h2>Welcome, {props.name}!</h2>; }
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Add 1</button> </div> ); }
This component shows how to track button clicks using state in a functional component. It updates the number each time you click the button.
import React, { useState } from 'react'; function ClickTracker() { const [clicks, setClicks] = useState(0); return ( <main> <h1>Click Tracker</h1> <p>You clicked {clicks} times.</p> <button onClick={() => setClicks(clicks + 1)} aria-label="Add one click">Click me</button> </main> ); } export default ClickTracker;
Always start component names with a capital letter to help React recognize them.
Use hooks like useState to add state inside functional components.
Functional components are easier to test and reuse than class components.
Functional components are simple JavaScript functions that return UI.
They can accept data through props and use hooks for state and effects.
They are the modern, recommended way to build React components.
Practice
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 AQuick Check:
Functional component = function returning JSX [OK]
- Confusing functional components with class components
- Thinking functional components are CSS or HTML tags
- Believing functional components must extend React.Component
Greeting?Solution
Step 1: Check function declaration syntax
function Greeting() { returnHello!
; } 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() { returnHello!
; } } is a class component, not functional; function Greeting { returnHello!
; } misses parentheses in function declaration.Final Answer:
function Greeting() { returnHello!
; } -> Option BQuick Check:
Function syntax with parentheses and return JSX [OK]
- Omitting parentheses in function declaration
- Confusing class components with functional components
- Assigning JSX directly without a function
function Welcome(props) {
return <h2>Hello, {props.name}!</h2>;
}
// Usage: <Welcome name="Alice" />Solution
Step 1: Understand props usage in functional components
The component receives props and uses props.name inside JSX to display the name.Step 2: Substitute props.name with passed value
Since name="Alice" is passed, {props.name} becomes "Alice" in the output.Final Answer:
<h2>Hello, Alice!</h2> -> Option DQuick Check:
props.name replaced by "Alice" in JSX output [OK]
- Thinking JSX shows literal {props.name} text
- Assuming props is undefined without passing
- Confusing component usage with rendered output
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}Solution
Step 1: Check useState usage
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 CQuick Check:
Hooks require import from React [OK]
- Forgetting to import useState
- Thinking setCount can't be called in event handlers
- Assuming JSX syntax errors without checking imports
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 AQuick 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]
- Trying to modify props directly
- Using class components instead of functional with hooks
- Relying on CSS hover for persistent selection
Ready to prove it?
Pass the test and this pattern moves unknown
