React async state and rollback recipes
Choose the React implementation for the state transition you need to protect: reject stale search responses or roll back a failed optimistic mutation. Each recipe links its saved requirement and captured broken/fixed source checks.
Download the React rule pack ZIP · Inspect the YAML rules
JavaScript
Requests can complete in a different order from the queries that started them. Aborting reduces wasted work; invalidating the old effect prevents a response already resolving from writing stale state. Protect the error branch as well as successful results.
Broken edit flagged · corrected edit passes
JavaScript
An optimistic UI can falsely display a completed transition after the server rejected it. Keep a snapshot, allow only one mutation at a time, and use the server-returned state on acknowledgement. A rejected operation restores the local snapshot and exposes its failure.
Broken edit flagged · corrected edit passes
Use a correction as a saved requirement
Choose the failure you need to prevent, add the scoped rule to your repository, and check both the broken and corrected implementation. Ask your agent to inspect the finding, repair the code and recheck the same rule. Keep framework tests beside the model check.
Perch keeps the requirement beside the code. Reuse the named check during the agent’s next edit and in CI, with cached answers free on unchanged requests.
Official React documentation · All language and framework recipes
Inspect the edit and the saved check
The Keep old React search responses from replacing the current query example checks src/useOrderSearch.js. The source below is the same example used by its linked capture.
Compare the broken edit with the corrected implementation
Broken implementation
import { useEffect, useState } from "react";
export function useOrderSearch(query) {
const [state, setState] = useState({items:[],error:null});
useEffect(() => {
let obsolete = false;
const controller = new AbortController();
setState({items:[],error:null});
fetch(`/api/orders?q=${encodeURIComponent(query)}`, {signal:controller.signal})
.then(response => { if (!response.ok) throw new Error("Search failed"); return response.json(); })
.then(items => { setState({items,error:null}); })
.catch(error => { if (!obsolete && error.name !== "AbortError") setState({items:[],error}); });
return () => { obsolete = true; controller.abort(); };
}, [query]);
return state;
}
Corrected implementation
import { useEffect, useState } from "react";
export function useOrderSearch(query) {
const [state, setState] = useState({items:[],error:null});
useEffect(() => {
let obsolete = false;
const controller = new AbortController();
setState({items:[],error:null});
fetch(`/api/orders?q=${encodeURIComponent(query)}`, {signal:controller.signal})
.then(response => { if (!response.ok) throw new Error("Search failed"); return response.json(); })
.then(items => { if (!obsolete) setState({items,error:null}); })
.catch(error => { if (!obsolete && error.name !== "AbortError") setState({items:[],error}); });
return () => { obsolete = true; controller.abort(); };
}, [query]);
return state;
}
Exact source and edit · Raw captured check
Make the agent check the requirement after its edit
Keep your existing perch.yaml. The scoped rule pack installs the saved requirement under .perch/rules/. Review its file selector and use the implementation’s integration prerequisites before adapting it to your application.
npm install -g @lakeday/perch
perch login
# Choose the setup command for your agent:
perch setup claude-code
# Or: perch setup codex
# Or: perch setup cursor
Check the edit and recheck the repair
curl -fL 'https://reporules.fyi/react/stale-request-cleanup/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check src/useOrderSearch.js --rules obsolete-search-does-not-update --json
# Read the finding, repair the implementation, then repeat:
perch check src/useOrderSearch.js --rules obsolete-search-does-not-update --json
Run the same check on the repaired working file before committing. Keep the application regression tests alongside it. Fresh model judgments can vary; an unchanged request can reuse its cached answer. A passing check does not prove application correctness.