Himanshu Srivastava · WeAreDevelopers India
function App() {
const [user] = useState(USER);
const [search, setSearch] = useState("");
return (
<AppContext.Provider value={{ user, search, setSearch }}>
<Dashboard />
</AppContext.Provider>
);
}
function Cell({ order }) {
const { user } = useContext(AppContext);
...
What the user feels
The tool that sees it
Where AI helps, and where it lies
Smallest change
Or it didn't happen
Every render is a snapshot. New objects, every time.
Those thin bars are 900 Cells · hover one: “Context changed”
Profiler › Save profile › whyslow reads the whole thing for you
function App() {
const [user] = useState(USER);
const [search, setSearch] = useState("");
return (
<AppContext.Provider value={{ user, search, setSearch }}>
<Dashboard /> // 900 Cells read this context
</AppContext.Provider>
);
}
function App() {
return (
<UserContext.Provider value={USER}>
<Dashboard /> // Cells only read the user
</UserContext.Provider>
);
}
function SearchPanel() {
const [search, setSearch] = useState(""); // lives here now
...
48 renders
48 renders
0 renders
whyslow: List is memo'd, but its `style` prop changed in 8 of 8 commits
List is memo'd, and it still rendered: Props changed: (style)
// 1 · useCallback, no memo → 48 renders const onSelect = useCallback(id => setSelected(id), []); <List onSelect={onSelect} style={{ padding: 8 }} /> // 2 · memo + inline style → 48 renders const MemoList = memo(List); <MemoList onSelect={onSelect} style={{ padding: 8 }} /> // 3 · memo + a stable style → 0 renders const STYLE = { padding: 8 }; // outside the component <MemoList onSelect={onSelect} style={STYLE} />
function Results({ query }) {
const options = { query, limit: 20 };
useEffect(() => {
fetchResults(options);
}, [options]);
}
function Results({ query }) {
useEffect(() => {
const options = { query, limit: 20 };
fetchResults(options);
}, [query]);
}
Profiler: a commit every 250 ms · Network tab: a request with every one
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(count + 1);
}, 400);
return () => clearInterval(id);
}, []);
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(c => c + 1);
}, 400);
return () => clearInterval(id);
}, []);
Found by: react-hooks lint, and AI reading the code
useEffect(() => {
const onMsg = () => setUnread(n => n + 1);
bus.on(onMsg);
// no cleanup
}, []);
useEffect(() => {
const onMsg = () => setUnread(n => n + 1);
bus.on(onMsg);
return () => bus.off(onMsg);
}, []);
Shout it: what does the AI say first?
Five guesses. None is the cause.
The cause. Then it asked for proof.
github.com/himanshuSri24/whyslow
| Trap | Measure | Plain | Compiled | |
|---|---|---|---|---|
| Render cascade | ms per keystroke | Fixed | ||
| Broken memo chain | row renders in 1 s | Fixed | ||
| Dependency arrays | requests in 2 s | Fixed | ||
| Stale closure | counter after 10 ticks | Still broken | ||
| Listener leak | handlers per message | Still broken |
Credit: [name]
| Typing or scrolling lags | Profiler + “why it rendered” |
| Requests nobody asked for | Network tab + Profiler timer |
| A value that's old or wrong | react-hooks lint, AI on the code |
| Slower the longer it's open | getEventListeners, heap snapshots |
| Did my fix work? | Same recording, before and after |
Thank you.
Himanshu Srivastava
devwithcoffee.com
Come argue
with me
on LinkedIn
useEffect(() => {
const ctrl = new AbortController();
fetch(url, { signal: ctrl.signal })
.then(r => r.json())
.then(setRows)
.catch(ignoreAbort);
return () => ctrl.abort();
}, [filter]);
Switch filter A → B. If A is slower, its response lands last and B's screen shows A's data.
Cleanup cancels the old request. The compiler can't help here either: it's logic.
"Existing memoization could not be preserved"
"Cannot access refs during render"
"This value cannot be modified"