25
loading...
This website collects cookies to deliver better user experience
npx create-react-app sandpack-demo
cd sandpack-demo
Note: Feel free to use the Yarn package manager if that is what you prefer.
npm install @codesandbox/sandpack-react
App.css
, App.test.js
, setupTests.js
, and logo.svg
. Also remove all the boilerplate code in App.js
. It should look like this -function App() {
return <div></div>;
}
export default App;
App.js
-import { Sandpack } from "@codesandbox/sandpack-react";
import "@codesandbox/sandpack-react/dist/index.css";
<Sandpack />
npm start
. Navigate to http://localhost:3000/ and this is what you should see - template
attribute and specify the value as react
-<Sandpack template="react" />
<Sandpack template="react" theme="sandpack-dark" />
App.js
looks like -import { Sandpack } from "@codesandbox/sandpack-react";
import "@codesandbox/sandpack-react/dist/index.css";
function App() {
return (
<div>
<Sandpack template="react" theme="sandpack-dark" />
</div>
);
}
export default App;
npx create-next-app sandpack-next-mdx-remote
cd sandpack-next-mdx-remote
Home.module.css
under the styles
directory and remove the boilerplate code in index.js
under the pages
directory. This is how it should look like -export default function Home() {
return <div></div>;
}
npm install next-mdx-remote
index.js
and add the following code -import { serialize } from "next-mdx-remote/serialize";
import { MDXRemote } from "next-mdx-remote";
export default function Home({ source }) {
return (
<div>
<MDXRemote {...source} />
</div>
);
}
export const getStaticProps = async () => {
const source = "```
html\n<h1>Hello World</h1>\n
```";
const mdxSource = await serialize(source);
return { props: { source: mdxSource } };
};
npm run dev
. Upon navigating to http://localhost:3000/, this is what our page should look like - code
element is being rendered nownpm install @codesandbox/sandpack-react
components
and add a file named CustomMDXCode.js
in there. Add the following code to that file -import { Sandpack } from "@codesandbox/sandpack-react";
import "@codesandbox/sandpack-react/dist/index.css";
const CustomMDXCode = props => {
return (
<Sandpack
template={props.template}
files={{ [`/${props.filename}`]: props.children }}
/>
);
};
export default CustomMDXCode;
/
to the beginning of the filename through string interpolation as it is required by Sandpack. index.js
file and make some changes to leverage the use of the new component -import { serialize } from "next-mdx-remote/serialize";
import { MDXRemote } from "next-mdx-remote";
import CustomMDXCode from "../components/CustomMDXCode";
export default function Home({ source }) {
return (
<div>
<MDXRemote
components={{ code: props => <CustomMDXCode {...props} /> }}
{...source}
/>
</div>
);
}
export const getStaticProps = async () => {
const source =
"```
js template=react filename=App.js\nexport default function App() {\n return <h1>Just some text...</h1>\n}\n
```";
const mdxSource = await serialize(source);
return { props: { source: mdxSource } };
};
javascript
, added a template
attribute and pointed that to react
, added a filename
attribute and named the file App.js
, and wrote a simple function that displays some text for the code part.