react-live

repository·master·Indexed 26 days ago

https://github.com/formidablelabs/react-live

A 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.

Tokens
3.8K
Snippets
9
Records
30
Agent score
87%

What's inside react-live

  1. Overview of react-live

    master
    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.
  2. Understand react-live internals and compatibility

    master

    The library uses Sucrase for transpilation, use-editable for code display, and prism-react-renderer for syntax highlighting. The LivePreview component performs a fake mount to render the transpiled code if it is a React component.

    Version Compatibility

    VersionSupported React versionEditorTranspiler
    v3.x.xv17.x.xuse-editableSucrase
    v2.x.xv16.x.xreact-simple-code-editorBublé
  3. Render JSX with React Live using Inline mode

    master

    By 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, and LivePreview to create an interactive editing environment.

    To implement this, wrap your editor and preview components in a LiveProvider and pass the source code to the code prop.

    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>
  4. Use React Hooks in React Live

    master

    React Live supports Hooks, but since only React is in the scope by default, you cannot destructure hooks like useState unless you explicitly add them to the scope.

    You have two options:

    1. Use the full namespace: React.useState()
    2. 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>
        </>
      );
    };
  5. Render multiple components or hooks using the `noInline` prop

    master

    If you need to render multiple components, define functional components, or use React hooks (like useState), use the noInline prop on the LiveProvider.

    When noInline is enabled, you must call the render() function at the end of your code block to specify which component or JSX element should be displayed in the LivePreview.

    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>
  6. Choose between react-live and component-playground

    master

    Deciding between react-live and component-playground depends on your specific requirements for bundle size, features, and setup speed:

    Featurereact-livecomponent-playground
    Best forSmaller bundle, SSR, and high flexibilityFast and easy setup/integration
    TranspilerSucrasebabel-standalone
    Bundle SizeSmaller (customized prism editor)Larger (familiar editor setup)
    FlexibilityHighly modular and customizableEasier/faster to set up
    Extra FeaturesMinimal out-of-the-boxRaw evaluation and pretty-printed output
    Error HandlingVariesPotentially more predictable (via react-dom)
  7. Use Babel for experimental features via transformCode

    master
    By default, react-live uses Sucrase for transpilation, which may not support certain experimental JavaScript features. To use babel instead, you can use the transformCode prop on the LiveProvider component to manually transform your code with babel before it reaches Sucrase.
  8. Install react-live

    master

    You can install react-live using npm or yarn. After installation, you can use the LiveProvider component to wrap your live editing environment, providing the initial code via the code prop. The environment is typically composed of LiveEditor, LiveError, and LivePreview components.

    import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live";
    
    <LiveProvider code="<strong>Hello World!</strong>">
      <LiveEditor />
      <LiveError />
      <LivePreview />
    </LiveProvider>;