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
- State holds the current value.
- JSX reads it:
value={name}tells the input what to display. - The user types, and React fires
onChange. - The handler calls
setNamewith the new text. - 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,
classNameandstyle. - Bind back from inputs with
onChangehandlers that call a setter. - Use
valuefor text and selects,checkedfor checkboxes.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.