React 19 Cheatsheet
React 19 Cheatsheet
React 19 Cheatsheet
React 19
ReactBootcamp.dev
React Compiler
converts to…
ReactBootcamp.dev
No More Memoization
const [count, setCount] = useState(0)
new React compiler
removes the need to use:
const increment = () => setCount((c) => c + 1)
return (
<>
<div>Count: {count}</div>
<div>Double Count: {doubleCount}</div>
<Button increment={increment} />
</>
)
ReactBootcamp.dev
No More forwardRef
import { forwardRef } from 'react'
ReactBootcamp.dev
Without forwardRef
function App() {
const buttonRef = useRef()
return (
<Button ref={buttonRef} onClick={onButtonClick}>
Click Me
</Button>
)
} access ref like any other prop
promises
context
use() replaces
multi-purpose hook
useEffect() useContext()
for data fetching for reading context
ReactBootcamp.dev
Fetch data - use()
function Person() {
const person = use(fetchPerson()) 1. resolve with use()
return <h1>{person.name}</h1>
}
3. display data in UI
function App() {
return (
<Suspense fallback={<h1>Loading...</h1>}> 2. show fallback
<Person />
</Suspense>
)
}
function User() {
const user = use(UserContext) replace useContext with use()
return <h1>Hello, {user}!</h1>
}
function App() {
return (
<UserContext.Provider value="Dave">
<User />
</UserContext.Provider>
)
}
ReactBootcamp.dev
Directives
commands to run React "use client" add to top of component
code on server or client
used already
in Next.js big, but simple
function myAction(formData) {}
<form action={formAction}>
ReactBootcamp.dev
Client Actions
"use client"
1. mark as client component
function App() {
function formAction(formData) { 3. get input value from formData
alert("You typed: " + formData.get("name"))
}
return (
<form action={formAction}> 2. connect function to action prop
<input type="text" name="name" />
<button type="submit">Submit</button>
</form>
)
}
ReactBootcamp.dev
useFormStatus()
it will usually take time
for actions to complete
disable submit
while pending
ReactBootcamp.dev
useFormStatus()
function Submit() {
const { pending } = useFormStatus() 2. get pending from hook
return (
<form action={formAction}>
<input name="name" />
<Submit /> 1. nest component inside form
</form>
)
}
ReactBootcamp.dev
useFormState()
What if you want the data
returned from your action?
Stateful hook
ReactBootcamp.dev
Basic - useFormState()
Preview
async function increment(previousState, formData) {
return previousState + 1
}
function StatefulForm() { 0
const [state, formAction] = useFormState(increment, 0)
Days without a new
return ( framework
<form>
{state} Increment
<button formAction={formAction}>Increment</button>
</form>
)
}
ReactBootcamp.dev
Basic - useFormState()
async function increment(previousState, formData) { 2. it gets prev state + formData
return previousState + 1
}
function StatefulForm() {
const [state, formAction] = useFormState(increment, 0) 1. hook takes action + initial value
return (
<form>
{state}
3. use returned state from action
<button formAction={formAction}>Increment</button>
</form>
)
}
ReactBootcamp.dev
Cart - useFormState()
Added to cart!
ReactBootcamp.dev
Cart - useFormState()
function addToCart(prevState, formData) {
const productId = formData.get("productId")
return productId === "1" ? "Added to cart!" : "Out of stock"
}
function App() {
const [message, formAction] = useFormState(addToCart, null) 1. call addToCart action
return (
<form action={formAction}>
<h2>My Product</h2>
<input type="hidden" name="productId" value="1" />
<button>Submit</button>
{message && <p>{message}</p>}
</form>
)
} ReactBootcamp.dev
Cart - useFormState()
function addToCart(prevState, formData) {
const productId = formData.get("productId")
return productId === "1" ? "Added to cart!" : "Out of stock" 3. return message
}
function App() {
const [message, formAction] = useFormState(addToCart, null) 1. call addToCart action
return (
<form action={formAction}>
<h2>My Product</h2>
<input type="hidden" name="productId" value="1" /> 2. pass productId (“1”) via input
<button>Submit</button>
{message && <p>{message}</p>} 4. display in UI
</form>
)
} ReactBootcamp.dev
useOptimistic()
What to do while we’re waiting
for action to finish running?
optimistic update
yeah lol
ReactBootcamp.dev
Chat - useOptimistic()
ReactBootcamp.dev
Chat - useOptimistic()
yeah lol
delivered
ReactBootcamp.dev
Chat - useOptimistic()
function ChatApp() {
const [messages, setMessages] = useState([]) 1. store messages in state