Data Binding Concepts
Data Binding
Section titled “Data Binding”What is Data Binding?
Section titled “What is Data Binding?”In React:
👉 Data binding = how data moves between UI (DOM) and JavaScript (state)
Types of Data Binding
Section titled “Types of Data Binding”There are mainly 2 types:
- One-way binding
- Two-way binding
1. One-Way Data Binding (React’s Default )
Section titled “1. One-Way Data Binding (React’s Default )”👉 Data flows in ONE direction State -> UI
How it works
Section titled “How it works”State -> UI (Render)👉 React controls everything from state
Example
Section titled “Example”function App() { const [name, setName] = React.useState("Prathamesh");
return <h1>{name}</h1>;}👉 Flow:
- State =
"Prathamesh" - UI shows it
Key Idea
Section titled “Key Idea”UI is just a reflection of state
2. Two-Way Data Binding
Section titled “2. Two-Way Data Binding”👉 Data flows BOTH ways State ↔ UI
Example (Conceptually)
Section titled “Example (Conceptually)”State -> Input fieldInput change -> State updateReact Way (Controlled Component)
Section titled “React Way (Controlled Component)”React doesn’t give automatic two-way binding like Angular 👉 You create it manually
Example
Section titled “Example”function App() { const [name, setName] = React.useState("");
return ( <input value={name} onChange={(e) => setName(e.target.value)} /> );}Flow Explained
Section titled “Flow Explained”State -> input valueUser types -> onChange -> updates stateState updates -> UI updates👉 Looks like two-way, but actually:
Warning: It’s still ONE-WAY + event handling
Important Insight (Very Important)
Section titled “Important Insight (Very Important)”👉 React ALWAYS follows:
One-way data flow
Even in “two-way binding” case:
- UI does NOT directly change state
- It sends an event -> then state updates
Visual Understanding
Section titled “Visual Understanding”One-way binding
Section titled “One-way binding”State -> UIReact “Two-way” (actual flow)
Section titled “React “Two-way” (actual flow)”State -> UIUI -> Event -> State update -> UI⚖️ React vs Angular (Quick Insight)
Section titled “⚖️ React vs Angular (Quick Insight)”- Angular -> true two-way binding (automatic)
- React -> controlled, explicit binding
👉 React gives more control and predictability
Why React Uses One-Way Binding
Section titled “Why React Uses One-Way Binding”- Easier debugging
- Predictable data flow
- Better performance
- Scalable architecture
Real Example (Putting It Together)
Section titled “Real Example (Putting It Together)”function Form() { const [email, setEmail] = React.useState("");
return ( <div> <input value={email} onChange={(e) => setEmail(e.target.value)} /> <p>Your email: {email}</p> </div> );}👉 Flow:
- Input updates state
- State updates UI
Final Understanding
Section titled “Final Understanding”React = One-way data flow “Two-way binding” = manually controlled via events