#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
State and Event Binding

Data Binding

Data binding in React means connecting your component's data, usually state or props, to what appears on screen, and connecting user actions back to that data. React uses one-way data flow: data goes from state into the JSX, and changes come back only through event handlers that call a state setter. Combining these two directions gives you the effect of two-way binding for form fields while keeping updates predictable.

Binding data into JSX

Curly braces in JSX insert any JavaScript expression. You can bind state into text, attributes, styles and class names.

import { useState } from 'react';

function ProfileCard() {
  const [name, setName] = useState('Aarav');
  const [isOnline, setIsOnline] = useState(true);
  const [fontSize, setFontSize] = useState(16);

  return (
    <div className={isOnline ? 'card online' : 'card'}>
      <h2 style={{ fontSize: `${fontSize}px` }}>{name}</h2>
      <p>Status: {isOnline ? 'Online' : 'Offline'}</p>

      {/* Two-way binding: value from state, changes back to state */}
      <input value={name} onChange={(e) => setName(e.target.value)} />

      <label>
        <input
          type="checkbox"
          checked={isOnline}
          onChange={(e) => setIsOnline(e.target.checked)}
        />
        Online
      </label>

      <input
        type="range"
        min="12"
        max="32"
        value={fontSize}
        onChange={(e) => setFontSize(Number(e.target.value))}
      />
    </div>
  );
}

Typing in the text box updates the heading instantly; ticking the checkbox changes the class and status text; dragging the slider resizes the heading. All three follow the same loop.

How the loop works

  1. State holds the current value.
  2. JSX reads it: value={name} tells the input what to display.
  3. The user types, and React fires onChange.
  4. The handler calls setName with the new text.
  5. React re-renders, and the input shows the updated state.

Because the input always shows exactly what state contains, you can transform values in the handler, for example forcing uppercase with setName(e.target.value.toUpperCase()), and the field reflects it immediately. This style of input is called a controlled component, which the next lesson explores.

Different controls bind to different props

Text inputs, textareas and selects bind through value and read e.target.value. Checkboxes and radio buttons bind through checked and read e.target.checked. Number and range inputs still give strings, so convert with Number(). In JSX, use className instead of class, and style takes an object with camelCase property names like fontSize, not a CSS string.

Why not let data flow both ways automatically?

Frameworks with automatic two-way binding update data from many directions, which can make it hard to tell what changed a value. In React, the only way state changes is by calling its setter. When something looks wrong, you search for that setter and find every place that could have changed it. Data passed to child components as props is read-only for the same reason; a child that needs to change parent data receives a callback function as a prop.

Classic mistakes

A field you cannot type in. Setting value={name} without an onChange handler makes the input read-only, because React keeps resetting it to the state value. React also logs a warning. Add a handler, or use defaultValue if you do not need control.

Calling the handler instead of passing it. onClick={toggle()} runs toggle during render. If it sets state, React renders again, calls it again, and you get an infinite loop error. Pass the function, onClick={toggle}, or wrap it in an arrow function.

Starting with undefined. useState() with no initial value makes the input uncontrolled at first, then controlled once you type, which also triggers a warning. Start text fields with ''.

Main ideas

  • Bind state into JSX with curly braces: text, attributes, className and style.
  • Bind back from inputs with onChange handlers that call a setter.
  • Use value for text and selects, checked for checkboxes.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course