Challenge - 5 Problems
Optimistic Updates Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat happens when an optimistic update is applied in Remix?
Consider a Remix app where a form submission triggers an optimistic update to the UI before the server confirms the change. What will the user see immediately after submitting the form?
Remix
function TodoItem({todo}) {
const [isDone, setIsDone] = React.useState(todo.done);
async function toggleDone() {
setIsDone(!isDone); // optimistic update
await fetch(`/todos/${todo.id}/toggle`, { method: 'POST' });
}
return <button onClick={toggleDone}>{isDone ? 'Done' : 'Not Done'}</button>;
}Attempts:
2 left
💡 Hint
Think about what optimistic update means: updating UI before server confirms.
✗ Incorrect
Optimistic updates update the UI immediately to improve user experience, assuming the server will succeed. Here, setIsDone updates the UI instantly.
❓ state_output
intermediateWhat is the final state after a failed optimistic update?
In Remix, if an optimistic update sets a todo item as done but the server returns an error, what should happen to the UI state?
Remix
function TodoItem({todo}) {
const [isDone, setIsDone] = React.useState(todo.done);
async function toggleDone() {
setIsDone(!isDone); // optimistic update
const response = await fetch(`/todos/${todo.id}/toggle`, { method: 'POST' });
if (!response.ok) {
setIsDone(isDone); // revert optimistic update
}
}
return <button onClick={toggleDone}>{isDone ? 'Done' : 'Not Done'}</button>;
}Attempts:
2 left
💡 Hint
Consider how the code handles a non-ok response.
✗ Incorrect
The code resets the state to the original isDone value if the server response is not ok, reverting the optimistic update.
📝 Syntax
advancedWhich option correctly implements an optimistic update with Remix's useFetcher?
Select the code snippet that correctly uses Remix's useFetcher hook to perform an optimistic update on a form submission.
Remix
import { useFetcher } from '@remix-run/react'; function LikeButton({postId, initialLikes}) { const fetcher = useFetcher(); const [likes, setLikes] = React.useState(initialLikes); function handleClick() { setLikes(likes + 1); // optimistic update fetcher.submit({postId}, {method: 'post', action: '/like'}); } return <button onClick={handleClick}>Likes: {likes}</button>; }
Attempts:
2 left
💡 Hint
Check how useFetcher is designed to submit forms.
✗ Incorrect
useFetcher.submit is the correct method to submit data. Optimistic update happens before submission to update UI immediately.
🔧 Debug
advancedWhy does this optimistic update fail to revert on server error?
This Remix component tries to revert an optimistic update on server error but fails. What is the bug?
Remix
function ToggleSwitch({initialOn}) {
const [isOn, setIsOn] = React.useState(initialOn);
async function toggle() {
setIsOn(!isOn); // optimistic update
const res = await fetch('/toggle', { method: 'POST' });
if (!res.ok) {
setIsOn(!isOn); // revert update
}
}
return <button onClick={toggle}>{isOn ? 'ON' : 'OFF'}</button>;
}Attempts:
2 left
💡 Hint
Think about what !isOn means at revert time.
✗ Incorrect
At revert time, !isOn is the new toggled state, so setting setIsOn(!isOn) again keeps the wrong state. The original state should be saved before toggling.
🧠 Conceptual
expertWhy use optimistic updates in Remix apps?
Which of the following best explains the main benefit of using optimistic updates in Remix applications?
Attempts:
2 left
💡 Hint
Think about how users feel when UI responds instantly.
✗ Incorrect
Optimistic updates make the app feel faster and more responsive by showing changes immediately, assuming success.
