A dashboard panel shows the time it was last refreshed. Clicking the Refresh button should update the timestamp. The button appears to do nothing - the timestamp never changes
The panel shows "Last refreshed: Never" on load. Clicking Refresh repeatedly has no effect - the timestamp never updates to the current time.
React doesn't always re-render when you call setState - when does it decide to skip?
React doesn't always re-render when you call setState - when does it decide to skip?
Why this fixes it
`setRefreshKey(refreshKey)` passed the current value of `refreshKey` back into state - React applies `Object.is` to compare the new value against the old one, and because they were identical primitives, it classified the update as a no-op and bailed out without re-rendering or re-running the effect. Changing the call to `setRefreshKey(prev => prev + 1)` produces a new integer on every click, one that is always different from the previous value. `Object.is(0, 1)` is `false`, so React proceeds with the re-render, the effect fires, and `setLastRefresh` updates the displayed timestamp.