React Live Playground

4.1.8 · active · verified Sun Apr 19

React Live is a versatile library for creating interactive, live-editable React code playgrounds within web applications. It allows developers to render React components dynamically, display their editable source code, and provide a real-time preview of the changes. The current stable version is 4.1.8, with patch releases occurring frequently to address fixes and minor improvements, as seen in recent version bumps (e.g., 4.1.0 to 4.1.8). Key differentiators include its modular structure, allowing for flexible styling and composition of its core components (`LiveProvider`, `LiveEditor`, `LiveError`, `LivePreview`), and its focus on being production-ready. It abstracts the complexities of code compilation and error handling, making it suitable for documentation sites, component libraries, and interactive learning platforms.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up a basic live code playground with an editable editor, a real-time preview, and error display using `react-live` components.

import React from 'react';
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';

const code = `
function Counter() {
  const [count, setCount] = React.useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

render(<Counter />);
`;

const scope = { React, useState: React.useState };

function MyLivePlayground() {
  return (
    <LiveProvider code={code} scope={scope}>
      <div style={{ display: 'flex', gap: '20px' }}>
        <div style={{ flex: 1 }}>
          <h3>Editor</h3>
          <LiveEditor style={{ minHeight: '200px', backgroundColor: '#333', color: '#fff', padding: '10px', borderRadius: '4px' }} />
        </div>
        <div style={{ flex: 1 }}>
          <h3>Preview</h3>
          <LivePreview style={{ border: '1px solid #ccc', padding: '10px', borderRadius: '4px' }} />
          <h3>Error</h3>
          <LiveError style={{ color: 'red', border: '1px solid red', padding: '10px', borderRadius: '4px' }} />
        </div>
      </div>
    </LiveProvider>
  );
}

export default MyLivePlayground;

view raw JSON →