Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
React Fundamentals
  1. 1.Introduction to React & Development Environment
  2. 2.JSX, Components & Props
  3. 3.Managing Component State with useState
  4. 4.Event Handling & Conditional Rendering
  5. 5.Rendering Lists & Basic useEffect
  6. 6.Deep Dive into useEffect Hook
  7. 7.useRef and useReducer for Complex State
  8. 8.Optimizing Performance with useMemo & useCallback
  9. 9.Context API & Custom Hooks for Reusability
  10. 10.Client-Side Routing with React Router
  11. 11.Forms, Validation & Data Fetching Patterns
  12. 12.Advanced Performance Optimization & Reconciliation
  13. 13.State Management Libraries (Redux Toolkit & Alternatives)
  14. 14.Testing React Components with Jest & React Testing Library
  15. 15.Advanced React Features & Modern Concepts
React Fundamentals
02

React · Lesson 2 of 15 · 3 min

JSX, Components & Props

Interviewers use these fundamentals to filter candidates. A shaky grasp here is a major red flag.

After this lesson, you will be able to explain how React turns JSX into a UI, how to structure an application with components, and how data flows between them. This is the foundation for every other React topic.

How JSX becomes DOM

JSX is not HTML in your JavaScript. It's syntactic sugar for React.createElement() function calls. Your build tool, using a transpiler like Babel, converts every JSX tag into one of these calls.

// This JSX:
<div className="profile">
  <h1>{user.name}</h1>
</div>

// Becomes this JavaScript:
React.createElement(
  "div",
  { className: "profile" },
  React.createElement("h1", null, user.name)
);

This transformation explains two fundamental rules. First, a component must return a single element because a function can only have one return value. Second, older codebases required import React from 'react' in every file with JSX, because the transpiled code explicitly called React.createElement.

In an interview, explaining this process shows you understand React's abstraction over the DOM, not just its syntax. It's a detail that separates candidates who know how from those who also know why.

Structuring UI with Components

Components are JavaScript functions that return JSX. They are the unit of composition and reuse in React. You build complex UIs by composing small, single-responsibility components.

The trap: Component Naming. Custom components must be named with PascalCase (e.g., UserProfile), while native HTML elements are lowercase (e.g., div). React uses this capitalization to decide whether to render a platform-specific tag or your custom component. If you name your component userProfile, React will try to render an <userprofile> HTML tag, which is not what you want.

The trap: The single root element. To return multiple elements without adding an unnecessary div to the DOM, use a React.Fragment or its shorthand, <></>. An extra div can break CSS flexbox or grid layouts and adds needless DOM depth. Fragments let you group elements without a wrapper node.

Passing Data with Props

Props (short for properties) are the arguments to your component function. They are passed as attributes in JSX and allow data to flow from parent to child. This is React's "unidirectional data flow."

The trap: Props are read-only. A component must never modify its own props. Doing so breaks the unidirectional data flow, making the application state unpredictable and hard to debug.

If an interviewer asks how a child component can update data, the answer is not "by changing its props." The correct pattern is for the parent to pass a callback function down to the child as a prop. The child calls this function to notify the parent that something needs to change. The parent then updates its own state, and the new value is passed back down to the child as a new prop.

Using the children Prop for Composition

React has a special prop, children, that contains whatever you pass between a component's opening and closing tags. This is the primary pattern for creating generic layout and wrapper components like modals, cards, or page layouts. The wrapper defines the "chrome" (border, title, buttons), and you pass the specific content in as children.

This is a powerful composition model. The Card component doesn't need to know or care what it's rendering.

// src/components/Card.tsx
import React from 'react';

interface CardProps {
  title: string;
  children: React.ReactNode;
}

function Card({ title, children }: CardProps) {
  const cardStyles: React.CSSProperties = {
    border: '1px solid #ccc',
    borderRadius: '8px',
    padding: '16px',
    margin: '10px',
    width: '300px',
    boxShadow: '0 2px 4px rgba(0,0,0,0.1)',
  };

  return (
    <div style={cardStyles}>
      <h3>{title}</h3>
      <div>
        {children} {/* Render whatever was passed between the tags */}
      </div>
    </div>
  );
}

// src/App.tsx
function App() {
  return (
    <div style={{ display: 'flex' }}>
      <Card title="User Profile">
        {/* This content becomes the 'children' prop */}
        <p><strong>Name:</strong> Ada Lovelace</p>
        <p>Pioneering computer programmer.</p>
      </Card>

      <Card title="Actions">
        {/* You can pass any valid JSX as children */}
        <p>Perform an action below.</p>
        <button onClick={() => alert('Action performed!')}>Click Me</button>
      </Card>
    </div>
  );
}

JSX attributes are not HTML attributes

JSX looks like HTML, but it's JavaScript. This leads to a few key differences in attribute naming that are frequent sources of bugs and signal a lack of hands-on experience.

  • className instead of class. Because class is a reserved keyword in JavaScript for creating classes.
  • htmlFor instead of for. for is also a reserved keyword for loops.
  • Inline style takes an object, not a string. Keys are camelCased: style={{ backgroundColor: 'red', fontSize: '16px' }}.
  • Event handlers are camelCased: onClick, onChange, onSubmit.

Try it

Create a UserProfile component that accepts a single user object as a prop. The user object should have name (string), avatarUrl (string), and bio (string). The component should render the user's image, name, and bio. Use it three times in your App with different user data.

Next: You've learned how to pass data down. Now you'll learn how components can manage their own data with Managing Component State with useState.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Introduction to React & Development Environment

Next

Managing Component State with useState