Skip to content

Sandpack v2 - SandpackProvider files not update #733

@nirtamir2

Description

@nirtamir2

Bug report

Packages affected

  • sandpack-client
  • sandpack-react

Description of the problem

I want to manage the files state for Sandpack in react state.
When I change the files state in v1.x - the SandpackPreview updated to reflect those changes
When I've done the same for Sandpack 2 - the SandpackPreview did not update according to file changes

What were you doing when the problem occurred?

I could use a workaround

function UpdateFiles(props: { files: SandpackFiles }) {
  const { files } = props;

  const { sandpack } = useSandpack();

  const stringifyFiles = JSON.stringify(files);
  useEffect(() => {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
    sandpack.updateFile(JSON.parse(stringifyFiles));
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [stringifyFiles]);

  return null;
}
 

and

      <SandpackProvider
        files={files}
        customSetup={{
          environment: "create-react-app",
        }}
      >
        <>
          <UpdateFiles files={files} />
          <SandpackPreview  />
        </>
      </SandpackProvider>

to force update the files. But this seems wrong. Also I had to do // eslint-disable-next-line react-hooks/exhaustive-deps and use only the stringifyFiles as deps - without using sandpack as dependency -
because sandpack instance change on each effect run - causing infinite rerender loop.

What steps can we take to reproduce the problem?

Take the provided codesandbox example - press the button to change the files - see if the preview reflect those changes

Link to sandbox:

Example of working example in v1.20

Example of preview not update in v2.06

Your Environment

Varies between 1/2, checked with chrome in macos

Software Name/Version
Sandpack-client version 2.01
Sandpack-react version 2.06
Browser Chrome
Operating System MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageNew issues that needs consideration

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions