Tài liệu tham khảo nhanh React
Tài liệu tham khảo React 19 có thể tìm kiếm và in được — component, JSX, hook, API Actions mới, context, ref và hiệu suất. Miễn phí.
Components & JSX
12function App() { return <h1>Hi</h1>; }
export default function App() {}
<Greeting name="Ada" />
{1 + 2} or {user.name}
<>{a}{b}</>
<div className="card">
{isOpen && <Menu />}
{ok ? <Yes /> : <No />}
{items.map(i => <li key={i.id}>{i.t}</li>)}
function Btn({ label, onClick }) {}
function Card({ children }) { return children; }
<input value={v} onChange={onChange} />
Rendering & setup
10import { createRoot } from 'react-dom/client'
createRoot(document.getElementById('root')).render(<App/>)
import { StrictMode } from 'react'
<StrictMode><App /></StrictMode>
import { hydrateRoot } from 'react-dom/client'
hydrateRoot(el, <App />)
root.unmount()
import App from './App.jsx'
<App {...props} />
export { Button, Card }
State & effects
12const [count, setCount] = useState(0)
setCount(c => c + 1)
setUser(u => ({ ...u, name }))
useEffect(() => { run(); }, [dep])
useEffect(() => { /* once */ }, [])
useEffect(() => () => cleanup(), [])
const ref = useRef(null)
const [state, dispatch] = useReducer(r, init)
const theme = useContext(ThemeContext)
const memo = useMemo(() => calc(a), [a])
useLayoutEffect(() => {}, [])
useSyncExternalStore(sub, get)
React 19 hooks & APIs
12import { use } from 'react'
const data = use(promise)
const theme = use(ThemeContext)
const [state, action, pending] = useActionState(fn, init)
const [opt, addOpt] = useOptimistic(state, reducer)
const { pending } = useFormStatus()
const [isPending, startTransition] = useTransition()
startTransition(async () => await save())
const deferred = useDeferredValue(value)
const id = useId()
<title>Page title</title>
function Input({ ref }) {}
Forms & Actions
12<form action={handleSubmit}>
function handleSubmit(formData) {}
formData.get('email')
const [state, action] = useActionState(submit, null)
<form action={action}>
const [s, a, pending] = useActionState(fn, init)
return { error: 'Invalid' }
function Submit() { const { pending } = useFormStatus() }
<button disabled={pending}>Save</button>
import { requestFormReset } from 'react-dom'
requestFormReset(formEl)
<form action={action}><Submit /></form>
Props & composition
10function Hi({ name = 'Guest' }) {}
function Box({ children }) { return children; }
<Box><p>Inside</p></Box>
function List({ render }) { return render(); }
<List render={() => <Item />} />
<Input {...props} />
const { id, ...rest } = props
<Avatar {...rest} size="lg" />
function Btn(props: BtnProps) {}
<Slot label={<b>Hi</b>} />
Events
10<button onClick={handleClick}>
function handleClick(e) {}
<input onChange={e => set(e.target.value)} />
<form onSubmit={e => e.preventDefault()}>
e.stopPropagation()
<li onClick={() => remove(id)}>
<input onKeyDown={e => e.key === 'Enter'}
<div onMouseEnter={hover}>
e.currentTarget.value
<button onClick={() => {}} type="button">
Lists & keys
10arr.map(x => <li key={x.id}>{x.t}</li>)
key={item.id}
key={index}
users.map(u => <Card key={u.id} {...u} />)
{list.length === 0 && <Empty />}
import { Fragment } from 'react'
<Fragment key={id}>{a}{b}</Fragment>
{rows.filter(r => r.on).map(...)}
Keys must be unique among siblings
{[...Array(n)].map((_, i) => ...)}
Context
10const ThemeContext = createContext('light')
import { createContext } from 'react'
<ThemeContext value={theme}>
<ThemeContext value={theme}><App /></ThemeContext>
const theme = useContext(ThemeContext)
const theme = use(ThemeContext)
if (cond) { const t = use(Ctx); }
export const ThemeContext = createContext()
<Ctx value={{ user, setUser }}>
Avoid new objects each render
Refs
10const inputRef = useRef(null)
<input ref={inputRef} />
inputRef.current.focus()
function Field({ ref }) { return <input ref={ref} /> }
<Field ref={inputRef} />
useImperativeHandle(ref, () => ({ focus }))
const setRef = node => { box = node; }
<div ref={setRef} />
const count = useRef(0)
ref={node => { return () => cleanup(); }}
Performance
10const Memo = memo(Component)
import { memo } from 'react'
const value = useMemo(() => calc(a), [a])
const fn = useCallback(() => f(a), [a])
const Lazy = lazy(() => import('./Big.jsx'))
import { lazy, Suspense } from 'react'
<Suspense fallback={<Spin />}><Lazy /></Suspense>
startTransition(() => setQuery(q))
const slow = useDeferredValue(query)
React Compiler auto-memoizes
Misc & metadata
10<Suspense fallback={<Loading />}>
class ErrorBoundary extends Component {}
<title>My Page</title>
<meta name="description" content="..." />
<link rel="stylesheet" href="..." />
<Comp key={id} />
React.lazy(() => import('./Page.jsx'))
createPortal(node, container)
{/* a comment */}
cleanup return in useEffect
Không có mục nào khớp với “:q”.
Giới thiệu về Tài liệu tham khảo nhanh React
Bảng tra cứu React này nhắm vào React 19 và ánh xạ thư viện theo khái niệm: component và JSX, render và thiết lập, state và effect, các hook cùng API của React 19, form và Actions, prop và composition, sự kiện, danh sách và key, context, ref, hiệu năng, cùng metadata và các nội dung khác.
Nó bao quát cả những nền tảng bạn dùng trong mọi component — useState, useEffect, prop, key, controlled input — lẫn những bổ sung mới của React 19, bao gồm các mẫu form dựa trên Actions, nên vừa hữu ích để ôn lại vừa là cầu nối sang các API hiện hành.
Bảng tra cứu này miễn phí và chạy phía trình duyệt. Lọc hook và mẫu ngay tại ô tìm kiếm, nhảy giữa các phần nhờ mục lục dính, sao chép mọi đoạn mã chỉ với một cú nhấp và in trang này để làm tài liệu tham khảo khi bạn xây dựng component.
Cách sử dụng Tài liệu tham khảo nhanh React
- Lướt qua các phần, từ Component & JSX và State & effect cho đến Hook & API của React 19 và Hiệu năng.
- Tìm một hook hoặc khái niệm như useEffect, key hoặc context để lọc bảng tra cứu ngay lập tức.
- Nhảy đến Form & Actions hoặc Ref qua thanh bên dính khi bạn cần một mẫu cụ thể.
- Nhấp vào một đoạn mã hoặc biểu tượng sao chép của nó để chép mã vào component của bạn.
- In bảng tra cứu để có tài liệu React offline.