React · Lesson 112 of 120
Cheat Sheet: Components
The essentials.
Let's pull everything together into quick reference sheets. First, components and JSX.
- Quick reference
- Components
- JSX
Components and JSX
A component is a function that returns JSX. Props pass data in, and children pass content in. Curly braces hold expressions. Map renders lists, with a key for each item. And conditions use and, or the ternary.
| Need | Write |
|---|---|
| A component | function Card(props) { return <div /> } |
| Props | <Card title="Hi" /> |
| Children | <Card>...</Card> |
| Expressions | {price * 1.2} |
| Lists | {items.map(i => <li key={i.id} />)} |
| Conditions | {ok && <p />} {a ? <A /> : <B />} |
Events and forms
Pass functions to event props. Controlled inputs pair value with on change. Checkboxes use checked. And forms handle on submit, calling prevent default first.
| Need | Write |
|---|---|
| Click | onClick={handleClick} |
| Text input | value={v} onChange={e => setV(e.target.value)} |
| Checkbox | checked={c} onChange={e => setC(e.target.checked)} |
| Form | onSubmit={e => { e.preventDefault(); }} |
Summary
That's components, props, lists, conditions, and forms on one page. Next, the hooks cheat sheet.
Key takeaways- Components
- Props
- Forms
Next up: Cheat Sheet: Hooks — the essentials