Edit the React example below and click Run. Use this to quickly test component logic and UI behavior.
React Tutorial
Learn React Tutorial with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>Welcome to React</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React TutorialReact Home
Learn React Home with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>Welcome to React</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React HomeReact Intro
Learn React Intro with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>Welcome to React</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React IntroReact Get Started
Learn React Get Started with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>Welcome to React</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Get StartedReact First App
Learn React First App with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const now = new Date().toLocaleTimeString(); | Declares code used in the example. |
return <p>My first app - {now}</p>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React First AppReact Render HTML
Learn React Render HTML with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>React Render HTML</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Render HTMLReact Upgrade
Learn React Upgrade with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>React Upgrade</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React UpgradeReact ES6
Learn React ES6 with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>React ES6</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React ES6React JSX Intro
Learn React JSX Intro with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <h2>Welcome to React</h2>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React JSX IntroReact JSX Expressions
Learn React JSX Expressions with a small example you can edit and run.
{expression}
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const a = 7, b = 5; | Declares code used in the example. |
return <p>{a} + {b} = {a + b}</p>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React JSX ExpressionsReact JSX Attributes
Learn React JSX Attributes with a small example you can edit and run.
<Button disabled={isDisabled} />
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const ok = true; | Declares code used in the example. |
return <button disabled={!ok}>Click</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React JSX AttributesReact JSX If Statements
Learn React JSX If Statements with a small example you can edit and run.
{condition ? <A /> : <B />}
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const loggedIn = false; | Declares code used in the example. |
return loggedIn ? <p>Welcome back</p> : <p>Please login</p>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React JSX If StatementsReact Components
Learn React Components with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function Badge({ text }){ return <span>{text}</span>; } | Declares code used in the example. |
function App(){ | Declares code used in the example. |
return <Badge text="Reusable" />; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React ComponentsReact Class
Learn React Class with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
class Counter extends React.Component { | Declares code used in the example. |
state = { count: 0 }; | JavaScript/JSX line. |
render(){ | JavaScript/JSX line. |
return <button onClick={() => this.setState({count:this.state.count+1})}>count: {this.state.count}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<Counter />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React ClassReact Props
Learn React Props with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function Hello(props){ return <h2>Hello, {props.name}</h2>; } | Declares code used in the example. |
ReactDOM.createRoot(document.getElementById('root')).render(<Hello name="Developer" />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React PropsReact Props Destructuring
Learn React Props Destructuring with a small example you can edit and run.
function Card({ title }) { return <h3>{title}</h3>; }
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function Card({ title }){ return <h3>{title}</h3>; } | Declares code used in the example. |
ReactDOM.createRoot(document.getElementById('root')).render(<Card title="Hello" />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Props DestructuringReact Props Children
Learn React Props Children with a small example you can edit and run.
function Box({ children }) { return <div>{children}</div>; }
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function Box({ children }){ return <div style={{border:'1px solid #ddd', padding:12}}>{children}</div>; } | Declares code used in the example. |
function App(){ | Declares code used in the example. |
return <Box><b>Children content</b></Box>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Props ChildrenReact Events
Learn React Events with a small example you can edit and run.
<button onClick={handleClick}>Click</button>
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [msg, setMsg] = React.useState('Click'); | Declares code used in the example. |
return <button onClick={() => setMsg('Clicked!')}>{msg}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React EventsReact Conditionals
Learn React Conditionals with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [open, setOpen] = React.useState(false); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<button onClick={() => setOpen(!open)}>{open ? 'Hide' : 'Show'}</button> | JavaScript/JSX line. |
{open && <p>Now visible</p>} | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React ConditionalsReact Lists
Learn React Lists with a small example you can edit and run.
{items.map(x => <li key={x.id}>{x.name}</li>)}
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const items = [{id:1,name:'React'},{id:2,name:'JSX'}]; | Declares code used in the example. |
return <ul>{items.map(x => <li key={x.id}>{x.name}</li>)}</ul>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React ListsReact Forms
Learn React Forms with a small example you can edit and run.
<input value={value} onChange={e => setValue(e.target.value)} />
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [value, setValue] = React.useState(''); | Declares code used in the example. |
return <input value={value} onChange={e => setValue(e.target.value)} placeholder="Type" />; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Forms power logins, checkout flows, and dashboards.
- 2Controlled inputs keep UI in sync.
- 1Not preventing default submit when needed.
- 2Storing duplicate form state.
- 1Prefer controlled inputs.
- 2Validate before submit.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React FormsReact Forms Submit
Learn React Forms Submit with a small example you can edit and run.
<input value={value} onChange={e => setValue(e.target.value)} />
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [name, setName] = React.useState(''); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<form onSubmit={(e)=>{e.preventDefault(); alert(name);}}> | JavaScript/JSX line. |
<input value={name} onChange={e => setName(e.target.value)} placeholder="Name" /> | JavaScript/JSX line. |
<button type="submit">Submit</button> | JavaScript/JSX line. |
</form> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Forms power logins, checkout flows, and dashboards.
- 2Controlled inputs keep UI in sync.
- 1Not preventing default submit when needed.
- 2Storing duplicate form state.
- 1Prefer controlled inputs.
- 2Validate before submit.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Forms SubmitReact Textarea
Learn React Textarea with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [text, setText] = React.useState('Hello'); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<textarea rows={3} value={text} onChange={e => setText(e.target.value)} /> | JavaScript/JSX line. |
<p>len: {text.length}</p> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Forms power logins, checkout flows, and dashboards.
- 2Controlled inputs keep UI in sync.
- 1Not preventing default submit when needed.
- 2Storing duplicate form state.
- 1Prefer controlled inputs.
- 2Validate before submit.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React TextareaReact Select
Learn React Select with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [lang, setLang] = React.useState('react'); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<select value={lang} onChange={e=>setLang(e.target.value)}> | JavaScript/JSX line. |
<option value="react">React</option> | JavaScript/JSX line. |
<option value="vue">Vue</option> | JavaScript/JSX line. |
</select> | JavaScript/JSX line. |
<p>{lang}</p> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
- 1Forms power logins, checkout flows, and dashboards.
- 2Controlled inputs keep UI in sync.
- 1Not preventing default submit when needed.
- 2Storing duplicate form state.
- 1Prefer controlled inputs.
- 2Validate before submit.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React SelectReact Multiple Inputs
Learn React Multiple Inputs with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [form, setForm] = React.useState({name:'', email:''}); | Declares code used in the example. |
const onChange = e => setForm({ ...form, [e.target.name]: e.target.value }); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<input name="name" value={form.name} onChange={onChange} placeholder="Name" /> | JavaScript/JSX line. |
<input name="email" value={form.email} onChange={onChange} placeholder="Email" /> | JavaScript/JSX line. |
<pre>{JSON.stringify(form, null, 2)}</pre> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Multiple InputsReact Checkbox
Learn React Checkbox with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [agree, setAgree] = React.useState(false); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<label> | JavaScript/JSX line. |
<input type="checkbox" checked={agree} onChange={e => setAgree(e.target.checked)} /> | JavaScript/JSX line. |
Agree: {String(agree)} | JavaScript/JSX line. |
</label> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Forms power logins, checkout flows, and dashboards.
- 2Controlled inputs keep UI in sync.
- 1Not preventing default submit when needed.
- 2Storing duplicate form state.
- 1Prefer controlled inputs.
- 2Validate before submit.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React CheckboxReact Radio
Learn React Radio with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [size, setSize] = React.useState('m'); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<label><input type="radio" checked={size==='s'} onChange={()=>setSize('s')} /> S</label> | JavaScript/JSX line. |
<label><input type="radio" checked={size==='m'} onChange={()=>setSize('m')} /> M</label> | JavaScript/JSX line. |
<p>size: {size}</p> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Forms power logins, checkout flows, and dashboards.
- 2Controlled inputs keep UI in sync.
- 1Not preventing default submit when needed.
- 2Storing duplicate form state.
- 1Prefer controlled inputs.
- 2Validate before submit.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React RadioReact Portals
Learn React Portals with a small example you can edit and run.
ReactDOM.createPortal(<Modal />, document.body)
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function Modal({ onClose }){ | Declares code used in the example. |
return ReactDOM.createPortal( | JavaScript/JSX line. |
<div style={{position:'fixed',inset:0,background:'rgba(0,0,0,.45)',display:'grid',placeItems:'center'}}> | JavaScript/JSX line. |
<div style={{background:'#fff',padding:16,borderRadius:10}}> | JavaScript/JSX line. |
<b>Portal Modal</b> | JavaScript/JSX line. |
<button onClick={onClose}>Close</button> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
</div>, | JavaScript/JSX line. |
document.body | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
function App(){ | Declares code used in the example. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React PortalsReact Suspense
Learn React Suspense with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
const LazyHello = React.lazy(() => new Promise(resolve => { | Declares code used in the example. |
setTimeout(() => resolve({ default: () => <h2>Loaded!</h2> }), 600); | JavaScript/JSX line. |
})); | JavaScript/JSX line. |
function App(){ | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<React.Suspense fallback={<p>Loading...</p>}> | JavaScript/JSX line. |
<LazyHello /> | JavaScript/JSX line. |
</React.Suspense> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React SuspenseReact Router
Learn React Router with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function Route({ path, children }){ | Declares code used in the example. |
return window.location.hash === path ? children : null; | JavaScript/JSX line. |
} | JavaScript/JSX line. |
function Link({ to, children }){ return <a href={to} style={{marginRight:10}}>{children}</a>; } | Declares code used in the example. |
function App(){ | Declares code used in the example. |
const [, force] = React.useState(0); | Declares code used in the example. |
React.useEffect(() => { | Effect hook: runs after render. |
const onHash = () => force(x => x + 1); | Declares code used in the example. |
window.addEventListener('hashchange', onHash); | JavaScript/JSX line. |
return () => window.removeEventListener('hashchange', onHash); | JavaScript/JSX line. |
}, []); | JavaScript/JSX line. |
return ( | JavaScript/JSX line. |
- 1Routing enables multi-page SPA navigation.
- 1Hardcoding route logic deep inside components.
- 1Keep routes centralized.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React RouterReact Transitions
Learn React Transitions with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [on, setOn] = React.useState(false); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<style>{`\n.box{width:110px;height:42px;background:#7c4dff;color:#fff;display:grid;place-items:center;border-radius:10px;transition:transform .25s,opacity .25s}\n.on{transform:translateX(120px)}\n.off{opacity:.7}\n`}</style> | JavaScript/JSX line. |
<button onClick={() => setOn(!on)}>Toggle</button> | JavaScript/JSX line. |
<div className={'box ' + (on ? 'on' : 'off')}>Move</div> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React TransitionsReact Forward Ref
Learn React Forward Ref with a small example you can edit and run.
const Input = React.forwardRef((props, ref) => <input ref={ref} />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
const FancyInput = React.forwardRef(function FancyInput(props, ref){ | Declares code used in the example. |
return <input ref={ref} placeholder="focus" />; | Returns JSX (UI). |
}); | JavaScript/JSX line. |
function App(){ | Declares code used in the example. |
const inputRef = React.useRef(null); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<FancyInput ref={inputRef} /> | JavaScript/JSX line. |
<button onClick={() => inputRef.current && inputRef.current.focus()}>Focus</button> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Forward RefReact HOC
Learn React HOC with a small example you can edit and run.
const Enhanced = withSomething(Component);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function withBorder(Component){ | Declares code used in the example. |
return function Wrapped(props){ | JavaScript/JSX line. |
return <div style={{border:'2px solid #00e5ff', padding:12, borderRadius:10}}><Component {...props} /></div>; | Returns JSX (UI). |
}; | JavaScript/JSX line. |
} | JavaScript/JSX line. |
function Hello({ name }){ return <h2>Hello {name}</h2>; } | Declares code used in the example. |
const Enhanced = withBorder(Hello); | Declares code used in the example. |
ReactDOM.createRoot(document.getElementById('root')).render(<Enhanced name="Mana" />); | Creates a root for rendering. |
- 1React is used to build interactive UIs for web apps and dashboards.
- 1Making components too large (hard to maintain).
- 1Prefer small components and clear state.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React HOCReact CSS Styling
Learn React CSS Styling with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<style>{`\n.badge{display:inline-block;padding:4px 10px;border-radius:999px;background:#00e5ff;color:#000;font-weight:700}\n`}</style> | JavaScript/JSX line. |
<span className="badge">Styled</span> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Styling systems keep UI consistent across screens.
- 1Overusing global styles that leak.
- 1Use a consistent naming and spacing system.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React CSS StylingReact CSS Modules
Learn React CSS Modules with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<style>{`\n.badge{display:inline-block;padding:4px 10px;border-radius:999px;background:#00e5ff;color:#000;font-weight:700}\n`}</style> | JavaScript/JSX line. |
<span className="badge">Styled</span> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Styling systems keep UI consistent across screens.
- 1Overusing global styles that leak.
- 1Use a consistent naming and spacing system.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React CSS ModulesReact CSS-in-JS
Learn React CSS-in-JS with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<style>{`\n.badge{display:inline-block;padding:4px 10px;border-radius:999px;background:#00e5ff;color:#000;font-weight:700}\n`}</style> | JavaScript/JSX line. |
<span className="badge">Styled</span> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Styling systems keep UI consistent across screens.
- 1Overusing global styles that leak.
- 1Use a consistent naming and spacing system.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React CSS-in-JSReact Sass
Learn React Sass with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
return <p>Sass compiles to CSS (use with a build tool).</p>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Styling systems keep UI consistent across screens.
- 1Overusing global styles that leak.
- 1Use a consistent naming and spacing system.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React SassReact Hooks
Learn React Hooks with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [count, setCount] = React.useState(0); | Declares code used in the example. |
return <button onClick={() => setCount(count + 1)}>count: {count}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React HooksWhat is Hooks?
Learn What is Hooks? with a small example you can edit and run.
function App(){ return <h2>Hello</h2>; }
ReactDOM.createRoot(root).render(<App />);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [count, setCount] = React.useState(0); | Declares code used in the example. |
return <button onClick={() => setCount(count + 1)}>count: {count}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React What is Hooks?React useState
Learn React useState with a small example you can edit and run.
const [state, setState] = React.useState(initialValue);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [open, setOpen] = React.useState(false); | Declares code used in the example. |
return <button onClick={() => setOpen(!open)}>{String(open)}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useStateReact useEffect
Learn React useEffect with a small example you can edit and run.
React.useEffect(() => { /* effect */ }, [deps]);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [n, setN] = React.useState(0); | Declares code used in the example. |
React.useEffect(() => { document.title = 'n=' + n; }, [n]); | Effect hook: runs after render. |
return <button onClick={() => setN(n + 1)}>n: {n}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useEffectReact useContext
Learn React useContext with a small example you can edit and run.
const value = React.useContext(SomeContext);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
const Theme = React.createContext('light'); | Declares code used in the example. |
function Badge(){ | Declares code used in the example. |
const t = React.useContext(Theme); | Declares code used in the example. |
return <span>theme: {t}</span>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
function App(){ | Declares code used in the example. |
return <Theme.Provider value="dark"><Badge /></Theme.Provider>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useContextReact useRef
Learn React useRef with a small example you can edit and run.
const ref = React.useRef(null);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const ref = React.useRef(null); | Declares code used in the example. |
return ( | JavaScript/JSX line. |
<div> | JavaScript/JSX line. |
<input ref={ref} /> | JavaScript/JSX line. |
<button onClick={() => ref.current && ref.current.focus()}>Focus</button> | JavaScript/JSX line. |
</div> | JavaScript/JSX line. |
); | JavaScript/JSX line. |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useRefReact useReducer
Learn React useReducer with a small example you can edit and run.
const [state, dispatch] = React.useReducer(reducer, initialState);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function reducer(state, action){ | Declares code used in the example. |
if(action.type === 'inc') return { count: state.count + 1 }; | JavaScript/JSX line. |
return state; | JavaScript/JSX line. |
} | JavaScript/JSX line. |
function App(){ | Declares code used in the example. |
const [state, dispatch] = React.useReducer(reducer, { count: 0 }); | Declares code used in the example. |
return <button onClick={() => dispatch({type:'inc'})}>count: {state.count}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useReducerReact useCallback
Learn React useCallback with a small example you can edit and run.
const fn = React.useCallback(() => {}, [deps]);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [n, setN] = React.useState(0); | Declares code used in the example. |
const inc = React.useCallback(() => setN(x => x + 1), []); | Declares code used in the example. |
return <button onClick={inc}>n: {n}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useCallbackReact useMemo
Learn React useMemo with a small example you can edit and run.
const memo = React.useMemo(() => compute(x), [x]);
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function App(){ | Declares code used in the example. |
const [items] = React.useState([3,1,2]); | Declares code used in the example. |
const total = React.useMemo(() => items.reduce((a,b) => a + b, 0), [items]); | Declares code used in the example. |
return <p>total: {total}</p>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React useMemoReact Custom Hooks
Learn React Custom Hooks with a small example you can edit and run.
function useX(){ /* hook */ }
Edit the React code below and click Run. The preview panel shows the rendered UI.
| Line | Meaning |
|---|---|
function useToggle(initial=false){ | Declares code used in the example. |
const [on, setOn] = React.useState(initial); | Declares code used in the example. |
const toggle = React.useCallback(() => setOn(x => !x), []); | Declares code used in the example. |
return [on, toggle]; | JavaScript/JSX line. |
} | JavaScript/JSX line. |
function App(){ | Declares code used in the example. |
const [on, toggle] = useToggle(); | Declares code used in the example. |
return <button onClick={toggle}>{on ? 'ON' : 'OFF'}</button>; | Returns JSX (UI). |
} | JavaScript/JSX line. |
ReactDOM.createRoot(document.getElementById('root')).render(<App />); | Creates a root for rendering. |
- 1Hooks let you reuse stateful logic across components.
- 2They are the modern default in React.
- 1Calling hooks conditionally.
- 2Forgetting dependencies in effects.
- 1Follow the Rules of Hooks.
- 2Keep dependency arrays accurate.
- 1Practice by editing the example and rerunning.
- 2Keep code readable first; optimize later.
- 1Change one UI detail and rerun.
- 2Add one edge case and handle it.
- 3Explain the result in 1 sentence.
Keywords (topic intent):
React React Custom Hooks