Skip to main content
Albero Academy

Lists, Events & Forms

Render lists with keys, handle events, build controlled form inputs, and lift state up to share it between components.

Chapter 4React15 min readBeginner friendly
  • #React
  • #Lists
  • #Forms

This chapter ties the fundamentals into the patterns you'll write every day: turning arrays into UI, showing things conditionally, handling user events, and building forms the React way. It closes with "lifting state up" — how sibling components share data.

Rendering lists & keys

Use map to turn an array into an array of elements. Each item needs a stable, unique key so React can track it across re-renders:

lists need keys
function ToolList({ tools }) {
return (
<ul>
{tools.map(tool => (
<li key={tool.id}>{tool.name}</li>
))}
</ul>
);
}

Conditional rendering

showing things conditionally
function Status({ placed }) {
return (
<div>
{placed && <span className="badge">Placed ✓</span>}
{placed ? <p>Congrats!</p> : <p>Keep going.</p>}
</div>
);
}

{cond && <X/>} renders X only when the condition is true; the ternary picks between two options.

Events

React events are camelCase and take a function, not a string. The handler receives a synthetic event object:

handling events
function Search() {
const handleClick = () => console.log("clicked");
const handleChange = (e) => console.log(e.target.value);
return (
<>
<button onClick={handleClick}>Search</button>
<input onChange={handleChange} />
</>
);
}

Controlled forms

In a controlled input, React state is the single source of truth: the input's value comes from state, and onChange writes back to state. Now the value is always available in your code:

a controlled form
function EnquiryForm() {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log({ name, email });
};
return (
<form onSubmit={handleSubmit}>
<input value={name} onChange={e => setName(e.target.value)} />
<input value={email} onChange={e => setEmail(e.target.value)} type="email" />
<button type="submit">Send</button>
</form>
);
}

Lifting state up

When two components need to share data, move that state to their closest common parent and pass it down as props — plus a callback to update it. The parent owns the state; the children read and request changes:

shared state lives in the parent
function Parent() {
const [query, setQuery] = useState("");
return (
<>
<SearchBox value={query} onChange={setQuery} />
<Results query={query} />
</>
);
}

Practice

Build a to-do app: a controlled input adds items to a state array (with real ids and keys), each item shows with a delete button, and a counter above reads the array length. This exercises lists, keys, events, forms and immutable state updates together.

Key takeaways

  1. 01Render lists with map and give each item a stable, unique key (not the array index).
  2. 02Use {cond && <X/>} or a ternary for conditional rendering.
  3. 03Events are camelCase and take a function; the handler gets a synthetic event.
  4. 04Controlled inputs bind value to state and write back on onChange — state is the source of truth.
  5. 05Share data between siblings by lifting state to their common parent and passing it down.