@immediately-run/sandpack-react
v2.22.0
Published
<img style="width:100%" src="https://user-images.githubusercontent.com/4838076/143581035-ebee5ba2-9cb1-4fe8-a05b-2f44bd69bb4b.gif" alt="Component toolkit for live running code editing experiences" />
Readme
Sandpack React
React components that give you the power of editable sandboxes that run in the browser.
import { Sandpack } from "@codesandbox/sandpack-react";
<Sandpack template="react" />;Documentation
For full documentation, visit https://sandpack.codesandbox.io/docs/
Migration: from files map to SandpackFS
Sandpack now stores file content inside a ZenFS-backed filesystem
(SandpackFS) instead of a synchronous Record<string, string> map. Most of
the existing APIs still accept the familiar files prop and materialize it
for you, but a few things changed that you should know about:
sandpack.filesis gone. Usesandpack.fileList(ordered array of paths),sandpack.fileMeta(per-filehidden/active/readOnly), andsandpack.fs(aSandpackFSinstance) to read content:const { sandpack } = useSandpack(); const code = await sandpack.fs.readFile(sandpack.activeFile);File mutations are asynchronous.
updateFile,addFile,deleteFile,resetFile,resetAllFiles, andupdateCurrentFileall returnPromise<void>now.useActiveCodeexposes anisLoadingflag for the initial read.options.fileResolverwas removed. Hand Sandpack a fully-formed filesystem via the newfsprop onSandpackProvider(or seed it viafiles) instead of providing a resolver callback.SandpackBundlerFilesis deprecated. It remains exported as a type alias for the{code, ...meta}snapshot shape that the bundler iframe protocol still consumes, but new code should useSandpackFilesInput(the input toSandpackFS.fromFiles) or operate onSandpackFSdirectly.
You can also pass a pre-built SandpackFS to SandpackProvider:
import { SandpackFS } from "@codesandbox/sandpack-client";
const fs = await SandpackFS.fromFiles({
"/index.js": { code: "console.log('hi')" },
});
<SandpackProvider fs={fs}>{/* ... */}</SandpackProvider>;