Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    Components and JSX · Lesson 2 of 21

    JSX rules

    One parent element, className, {} for JavaScript, and style as an object.

    8 minutes3 games

    JSX looks like HTML but is JavaScript. A tool called Babel turns <h1>Hi</h1> into React.createElement("h1", null, "Hi"). That's why a few rules differ from HTML:

    HTMLJSXWhy

    class="card"

    className="card"

    class is reserved in JS

    for="email"

    htmlFor="email"

    for is reserved too

    <br>

    <br />

    every tag must close

    onclick="…"

    onClick={fn}

    camelCase, and you pass a function

    style="color: red"

    style={{ color: 'red' }}

    an object, not text

    React
    function App() {
      const student = { name: "Nour", xp: 410 }
      const level = Math.floor(student.xp / 100)
    
      return (
        <>
          <h2 className="title">{student.name.toUpperCase()}</h2>
          <p style={{ color: "teal", fontWeight: "bold" }}>
            Level {level}, {student.xp >= 400 ? "top student" : "keep going"}
          </p>
          <label htmlFor="note">Note</label>
          <input id="note" placeholder="Write something" />
        </>
      )
    }
    • Anything between { } is a JavaScript expression: a variable, a calculation, a function call, a ternary. Not if or for (those are statements).
    • A component must return one parent element. If you don't want an extra div, use a Fragment <>…</>.
    • style takes an object with camelCase keys: fontWeight, not font-weight.

    Important

    React escapes any text you put between { }, so a user typing <script> sees text, not running code. That protects you from XSS. The one exception is dangerouslySetInnerHTML, and the name is a warning: never use it with user content that hasn't been sanitized.

    Spot the bug

    This code won't work. Which line has the problem?

    Complete the code

    Complete it so the class applies and the text is red.

    function App() {
      return <p ="warning" style={{ color:  }}>Careful!</p>
    }

    Interview question

    What does JSX compile to, and why can't you write if inside { }?

    Exercise

    You have const price = 450 and const discount = 0.2. Show Final price: 360 EGP in a <p>, computing the number inside { }.

    The page should show: Final price: 360 EGP

    React
    function App() {
      const price = 450
      const discount = 0.2
      return <p>Final price: EGP</p>
    }

    Learn more from

    • react.dev: Writing markup with JSXreact.dev
    • react.dev: JavaScript in JSX with curly bracesreact.dev
    • MDN: Cross-site scripting (XSS)developer.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousWhy React?NextProps
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.