Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(recorder): update to React 18 #32101

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/recorder/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
import '@web/common.css';
import { applyTheme } from '@web/theme';
import '@web/third_party/vscode/codicon.css';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as ReactDOM from 'react-dom/client';
import { Main } from './main';

(async () => {
applyTheme();
ReactDOM.render(<Main/>, document.querySelector('#root'));
ReactDOM.createRoot(document.querySelector('#root')!).render(<Main/>);
})();
14 changes: 8 additions & 6 deletions packages/recorder/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ export const Main: React.FC = ({
window.playwrightSetSources = setSources;
window.playwrightSetPaused = setPaused;
window.playwrightUpdateLogs = callLogs => {
const newLog = new Map<string, CallLog>(log);
Copy link
Member Author

@Skn0tt Skn0tt Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this closes over log from the outer scope, at the point in time where the closure is created. If log changes, e.g. because setLog is called, this will only be picked up if the component rerenders and the callback is reset. Before Concurrent React, apparently this component was rerendered often enough. With Concurrent mode that doesn't work anymore, though, and log sometimes points to an old version of the logs, dropping updates that were applied in between renders :/

The fix is to use an updater function so we always update based on the current version of the state, no matter when Main is rerendered.

Docs on updater function: https://react.dev/reference/react/useState#updating-state-based-on-the-previous-state

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great description!

for (const callLog of callLogs) {
callLog.reveal = !log.has(callLog.id);
newLog.set(callLog.id, callLog);
}
setLog(newLog);
setLog(log => {
const newLog = new Map<string, CallLog>(log);
for (const callLog of callLogs) {
callLog.reveal = !log.has(callLog.id);
newLog.set(callLog.id, callLog);
}
return newLog;
});
};

window.playwrightSourcesEchoForTest = sources;
Expand Down
Loading