react-live
repository·master·Indexed 26 days ago
https://github.com/formidablelabs/react-liveA flexible playground for live editing React code, enabling developers to render components with editable source code and a live preview. It provides a modular set of components including LiveProvider, LiveEditor, LiveError, and LivePreview, and supports TypeScript and custom globals via a scope prop. The library uses Sucrase for transpilation and prism-react-renderer for syntax highlighting.
What's inside react-live
- React Live is a flexible playground for live editing React code. It allows you to render React components with editable source code and a live preview. The library is designed to be modular, allowing you to style and compose its components freely.
Understand react-live internals and compatibility
masterThe library uses Sucrase for transpilation,
use-editablefor code display, andprism-react-rendererfor syntax highlighting. TheLivePreviewcomponent performs a fake mount to render the transpiled code if it is a React component.Version Compatibility
Version Supported React version Editor Transpiler v3.x.x v17.x.x use-editableSucrasev2.x.x v16.x.x react-simple-code-editorBubléRender JSX with React Live using Inline mode
masterBy default, React Live treats the provided code block as a single JSX expression that is rendered as if it were returned from a functional component. Use
LiveProvider,LiveEditor, andLivePreviewto create an interactive editing environment.To implement this, wrap your editor and preview components in a
LiveProviderand pass the source code to thecodeprop.import { LiveProvider, LiveEditor, LivePreview } from "react-live"; <LiveProvider code={code}> <div className="grid grid-cols-2 gap-4"> <LiveEditor className="font-mono" /> <LivePreview /> </div> </LiveProvider>Use React Hooks in React Live
masterReact Live supports Hooks, but since only
Reactis in the scope by default, you cannot destructure hooks likeuseStateunless you explicitly add them to thescope.You have two options:
- Use the full namespace:
React.useState() - Add the hook to the scope:
scope={{ useState, ... }}
// Option 1: Using the React namespace (default scope) () => { const [likes, increaseLikes] = React.useState(0); return ( <> <p>{`${likes} likes`}</p> <button onClick={() => increaseLikes(likes + 1)}>Like</button> </> ); };- Use the full namespace:
Install react-live via npm
masterYou can installreact-liveusing npm to add a live React code playground to your application. It supports UMD, CJS, and ESM module formats.Render multiple components or hooks using the `noInline` prop
masterIf you need to render multiple components, define functional components, or use React hooks (like
useState), use thenoInlineprop on theLiveProvider.When
noInlineis enabled, you must call therender()function at the end of your code block to specify which component or JSX element should be displayed in theLivePreview.import { LiveProvider, LiveEditor, LivePreview } from "react-live"; // The code passed to LiveProvider must end with render(<Component />) <LiveProvider code={code} noInline> <div className="grid grid-cols-2 gap-4"> <LiveEditor className="font-mono" /> <LivePreview /> </div> </LiveProvider>Choose between react-live and component-playground
masterDeciding between
react-liveandcomponent-playgrounddepends on your specific requirements for bundle size, features, and setup speed:Feature react-livecomponent-playgroundBest for Smaller bundle, SSR, and high flexibility Fast and easy setup/integration Transpiler Sucrasebabel-standaloneBundle Size Smaller (customized prismeditor)Larger (familiar editor setup) Flexibility Highly modular and customizable Easier/faster to set up Extra Features Minimal out-of-the-box Raw evaluation and pretty-printed output Error Handling Varies Potentially more predictable (via react-dom)Use Babel for experimental features via transformCode
masterBy default,react-liveusesSucrasefor transpilation, which may not support certain experimental JavaScript features. To usebabelinstead, you can use thetransformCodeprop on theLiveProvidercomponent to manually transform your code withbabelbefore it reachesSucrase.Run the website in local development mode
masterStart a local development server to preview the website. This command opens a browser window and supports live reloading, so most changes are reflected immediately without a server restart.
$ yarn startInstall react-live
masterYou can install
react-liveusing npm or yarn. After installation, you can use theLiveProvidercomponent to wrap your live editing environment, providing the initial code via thecodeprop. The environment is typically composed ofLiveEditor,LiveError, andLivePreviewcomponents.import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live"; <LiveProvider code="<strong>Hello World!</strong>"> <LiveEditor /> <LiveError /> <LivePreview /> </LiveProvider>;Install the website dependencies
masterTo install the necessary dependencies for the website, run the following command in the root directory of the website package.$ yarnBuild the website for production
masterGenerate static content for the website. The output will be placed in the
builddirectory, which can then be served by any static hosting service.$ yarn build