Bird
Raised Fist0
Reactframework~10 mins

Functional components in React - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Functional components
Start: Define Functional Component
↓
Call Functional Component
↓
Execute Function Body
↓
Return JSX Element
↓
React Renders JSX to DOM
↓
Component Displayed on Screen
This flow shows how React calls a functional component, runs its code, returns JSX, and renders it on the screen.
Execution Sample
React
function Greeting() {
  return <h1>Hello, friend!</h1>;
}

ReactDOM.render(<Greeting />, document.getElementById('root'));
This code defines a simple functional component that returns a greeting message and renders it.
Execution Table
StepActionEvaluationResult
1Call Greeting()Function startsEnter function body
2Return JSX <h1>Hello, friend!</h1>JSX createdJSX element ready
3React renders JSXConvert JSX to DOM nodesDOM element <h1> with text 'Hello, friend!'
4Display on screenBrowser shows elementUser sees 'Hello, friend!' on page
5No more codeComponent finishedRender complete
💡 Component returns JSX and React finishes rendering it to the screen.
Variable Tracker
VariableStartAfter CallAfter ReturnFinal
GreetingFunction definitionFunction executingJSX returnedRendered on screen
Key Moments - 2 Insights
Why does the functional component return JSX instead of HTML?
JSX is a special syntax React uses to describe UI elements. React converts JSX to real HTML in the browser. See execution_table step 2 and 3.
What happens if the functional component does not return anything?
React will render nothing for that component. The execution_table shows the return step is essential to produce visible output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 3?
AJSX element ready
BDOM element <h1> with text 'Hello, friend!'
CFunction starts
DUser sees 'Hello, friend!' on page
💡 Hint
Check the 'Result' column in row for step 3.
At which step does React convert JSX to actual DOM nodes?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Evaluation' columns for step 3.
If the component returned null instead of JSX, what would happen?
AReact renders an empty space
BReact throws an error
CReact renders the previous content
DReact renders the JSX anyway
💡 Hint
Refer to key_moments about what happens if nothing is returned.
Concept Snapshot
Functional components are simple JavaScript functions.
They return JSX describing UI elements.
React calls them to get JSX and renders it.
No class syntax or lifecycle methods needed.
Use them to build UI pieces easily.
Full Transcript
Functional components in React are JavaScript functions that return JSX, a syntax that looks like HTML but is used by React to describe UI elements. When React calls a functional component, it runs the function body and expects JSX to be returned. React then converts this JSX into real DOM elements and displays them on the screen. This process is simple and direct, making functional components easy to write and understand. If a functional component does not return JSX or returns null, React will render nothing for that component. This visual execution trace shows each step from calling the function to rendering the output, helping beginners see how React handles functional components.

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

  1. Step 1: Understand React component types

    React components can be functions or classes that return UI elements.
  2. Step 2: Identify functional component definition

    A functional component is a plain JavaScript function returning JSX, not a class or style.
  3. Final Answer:

    A JavaScript function that returns JSX to render UI -> Option A
  4. 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

  1. Step 1: Check function declaration syntax

    function Greeting() { return

    Hello!

    ; } correctly declares a function with parentheses and returns JSX.
  2. 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.
  3. Final Answer:

    function Greeting() { return

    Hello!

    ; }
    -> Option B
  4. 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?
function Welcome(props) {
  return <h2>Hello, {props.name}!</h2>;
}

// Usage: <Welcome name="Alice" />
medium
A. <Welcome name="Alice" />
B. Hello, {props.name}!
C. Error: props is undefined
D. <h2>Hello, Alice!</h2>

Solution

  1. Step 1: Understand props usage in functional components

    The component receives props and uses props.name inside JSX to display the name.
  2. Step 2: Substitute props.name with passed value

    Since name="Alice" is passed, {props.name} becomes "Alice" in the output.
  3. Final Answer:

    <h2>Hello, Alice!</h2> -> Option D
  4. Quick Check:

    props.name replaced by "Alice" in JSX output [OK]
Hint: Props values replace placeholders inside JSX [OK]
Common Mistakes:
  • Thinking JSX shows literal {props.name} text
  • Assuming props is undefined without passing
  • Confusing component usage with rendered output
4. Identify the error in this React functional component:
function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
medium
A. Missing return statement
B. Incorrect JSX syntax inside return
C. useState is not imported from React
D. setCount should not be called inside onClick

Solution

  1. Step 1: Check useState usage

    useState is a React hook and must be imported explicitly from 'react'.
  2. Step 2: Verify other code parts

    The return statement and JSX syntax are correct; calling setCount in onClick is valid.
  3. Final Answer:

    useState is not imported from React -> Option C
  4. 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

  1. Step 1: Understand state and props roles

    Props provide data (items), state tracks UI changes (selected item).
  2. Step 2: Choose correct React hooks usage

    useState is used to track selected item; mapping props.items with onClick updates state correctly.
  3. Step 3: Evaluate other options

    Class components are outdated here; useEffect should not modify props; CSS :hover can't track selection state.
  4. Final Answer:

    Use useState to track selected item and map props.items to render list with onClick handlers updating state -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard