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

fix(client): avoid concurrent compile step and init #946

Merged
merged 4 commits into from
May 26, 2023
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
Next Next commit
Update 3 files
  • Loading branch information
danilowoz committed May 22, 2023
commit f6c775f2b9fd49dd6aac4cace0d496bf3bfc3064
33 changes: 33 additions & 0 deletions sandpack-react/src/Issues.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { useState } from "react";

import { REACT_TEMPLATE } from "./templates";

import {
Sandpack,
SandpackCodeEditor,
Expand All @@ -13,6 +15,37 @@ export default {
title: "Bug reports/Issues",
};

export const Issue944 = (): JSX.Element => {
const [files, setFiles] = useState(REACT_TEMPLATE["files"]);

return (
<>
<button
onClick={() => {
setFiles(REACT_TEMPLATE["files"]);
}}
>
1
</button>
<button
onClick={() => {
setFiles({
...REACT_TEMPLATE["files"],
"/App.js": `import Foo from "./Foo";
export default function App() {
return <Foo />
}`,
"Foo.js": `export default () => <button> Foo</button>`,
});
}}
>
2
</button>
<Sandpack files={files} template="react" />
</>
);
};

export const FlushServerVsClient = (): JSX.Element => {
return (
<SandpackProvider>
Expand Down
6 changes: 6 additions & 0 deletions sandpack-react/src/contexts/utils/useClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ export const useClient: UseClient = (
return;
}

console.log(recompileMode, filesState);

/**
* When the environment changes, Sandpack needs to make sure
* to create a new client and the proper bundler
Expand Down Expand Up @@ -507,6 +509,10 @@ export const useClient: UseClient = (
});
}, recompileDelay);
}

return () => {
window.clearTimeout(debounceHook.current);
};
},
[
filesState.files,
Expand Down
1 change: 1 addition & 0 deletions sandpack-react/src/contexts/utils/useFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const useFiles: UseFiles = (props) => {
const isMountedRef = useRef(false);
useEffect(() => {
if (isMountedRef.current) {
console.log("changed");
setState(getSandpackStateFromProps(props));
} else {
isMountedRef.current = true;
Expand Down