Convert Figma logo to code with AI

FormidableLabs logoreact-live

A flexible playground for live editing React components

4,411
250
4,411
24

Top Related Projects

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/

React Ace Component

Monaco Editor for React.

Simple no-frills code editor with syntax highlighting

syntax highlighting component for react with prismjs or highlightjs ast using inline styles

Quick Overview

React-live is a flexible and interactive code editor component for React applications. It allows developers to create live code playgrounds within their projects, enabling real-time code editing and execution. This library is particularly useful for creating interactive documentation, tutorials, or code demonstrations.

Pros

  • Provides a seamless, interactive coding experience within React applications
  • Supports custom syntax highlighting and theming
  • Offers flexibility with customizable error boundaries and scope
  • Enables live code editing and execution in real-time

Cons

  • May have a learning curve for developers unfamiliar with code editor components
  • Limited to React-based projects
  • Potential performance impact when rendering complex code examples
  • Requires careful consideration of security implications when allowing user input

Code Examples

  1. Basic usage of LiveProvider and LiveEditor:
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live'

const code = `
function App() {
  return (
    <h1>Hello, React-live!</h1>
  )
}
`

function MyComponent() {
  return (
    <LiveProvider code={code}>
      <LiveEditor />
      <LiveError />
      <LivePreview />
    </LiveProvider>
  )
}
  1. Using custom theme and language:
import { LiveProvider, LiveEditor } from 'react-live'
import { dracula } from 'prism-react-renderer/themes/dracula'

const code = `const greeting = 'Hello, World!';
console.log(greeting);`

function CustomEditor() {
  return (
    <LiveProvider code={code} language="javascript" theme={dracula}>
      <LiveEditor />
    </LiveProvider>
  )
}
  1. Implementing a custom scope:
import { LiveProvider, LiveEditor, LivePreview } from 'react-live'
import { Button } from './components'

const code = `
function App() {
  return (
    <Button onClick={() => alert('Clicked!')}>
      Click me
    </Button>
  )
}
`

function ScopedExample() {
  return (
    <LiveProvider code={code} scope={{ Button }}>
      <LiveEditor />
      <LivePreview />
    </LiveProvider>
  )
}

Getting Started

To use React-live in your project, follow these steps:

  1. Install the package:
npm install react-live
  1. Import the necessary components:
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live'
  1. Set up a basic live code editor:
const code = `function Greeting() { return <h1>Hello, World!</h1> }`

function App() {
  return (
    <LiveProvider code={code}>
      <LiveEditor />
      <LiveError />
      <LivePreview />
    </LiveProvider>
  )
}

This setup creates a live code editor with error handling and a preview of the rendered component.

Competitor Comparisons

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/

Pros of react-codemirror

  • More comprehensive code editing features, including syntax highlighting for multiple languages
  • Better performance for large code files
  • Extensive customization options for appearance and behavior

Cons of react-codemirror

  • Steeper learning curve due to more complex API
  • Larger bundle size, which may impact load times
  • Less focused on live code execution compared to react-live

Code Comparison

react-codemirror:

import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';

<CodeMirror
  value="console.log('hello world!');"
  height="200px"
  extensions={[javascript({ jsx: true })]}
  onChange={(value, viewUpdate) => {
    console.log('value:', value);
  }}
/>

react-live:

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

<LiveProvider code="<strong>Hello, world!</strong>">
  <LiveEditor />
  <LiveError />
  <LivePreview />
</LiveProvider>

react-codemirror offers more advanced code editing capabilities, while react-live focuses on live code execution and preview. react-codemirror is better suited for complex code editing tasks, while react-live excels in interactive code demonstrations and documentation.

React Ace Component

Pros of react-ace

  • Specialized for code editing with syntax highlighting and autocompletion
  • Supports multiple programming languages out of the box
  • Offers more advanced code editing features like line wrapping and themes

Cons of react-ace

  • Less flexible for general-purpose live editing scenarios
  • Heavier dependency due to the full Ace editor integration
  • May have a steeper learning curve for basic use cases

Code Comparison

react-ace:

import AceEditor from 'react-ace';
import 'ace-builds/src-noconflict/mode-javascript';
import 'ace-builds/src-noconflict/theme-github';

<AceEditor
  mode="javascript"
  theme="github"
  onChange={onChange}
  name="UNIQUE_ID_OF_DIV"
  editorProps={{ $blockScrolling: true }}
/>

react-live:

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

<LiveProvider code={code}>
  <LiveEditor />
  <LiveError />
  <LivePreview />
</LiveProvider>

react-ace is better suited for dedicated code editing with advanced features, while react-live offers a more streamlined approach for live code editing and preview, especially for React components. The choice between them depends on the specific requirements of your project and the level of code editing functionality needed.

Monaco Editor for React.

Pros of react-monaco-editor

  • Full-featured code editor with advanced capabilities like IntelliSense, syntax highlighting, and error checking
  • Supports multiple programming languages out of the box
  • Highly customizable with extensive configuration options

Cons of react-monaco-editor

  • Larger bundle size due to the comprehensive feature set
  • Steeper learning curve for implementation and customization
  • May be overkill for simple code editing tasks

Code Comparison

react-monaco-editor:

import MonacoEditor from 'react-monaco-editor';

<MonacoEditor
  width="800"
  height="600"
  language="javascript"
  theme="vs-dark"
  value={code}
  options={options}
  onChange={handleEditorChange}
/>

react-live:

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

<LiveProvider code={code} scope={scope}>
  <LiveEditor />
  <LiveError />
  <LivePreview />
</LiveProvider>

react-monaco-editor offers a more powerful editing experience with advanced features, while react-live provides a simpler, lightweight solution focused on live code editing and preview. The choice between them depends on the specific requirements of your project, such as the need for advanced editing capabilities versus a more streamlined, interactive coding environment.

Simple no-frills code editor with syntax highlighting

Pros of react-simple-code-editor

  • Lightweight and focused on basic code editing functionality
  • Easy to integrate and customize
  • Minimal dependencies, resulting in a smaller bundle size

Cons of react-simple-code-editor

  • Limited features compared to react-live (e.g., no live preview or execution)
  • Less suitable for complex code editing scenarios
  • Requires additional libraries for syntax highlighting

Code Comparison

react-simple-code-editor:

import Editor from 'react-simple-code-editor';
import { highlight, languages } from 'prismjs/components/prism-core';

<Editor
  value={code}
  onValueChange={code => setCode(code)}
  highlight={code => highlight(code, languages.js)}
  padding={10}
  style={{
    fontFamily: '"Fira code", "Fira Mono", monospace',
    fontSize: 12,
  }}
/>

react-live:

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

<LiveProvider code={code} scope={scope}>
  <LiveEditor />
  <LiveError />
  <LivePreview />
</LiveProvider>

The code comparison shows that react-simple-code-editor focuses on basic editing functionality, while react-live provides a more comprehensive solution with live preview and error handling capabilities. react-live's approach is more suitable for interactive code demonstrations and tutorials, whereas react-simple-code-editor is better for simpler code editing tasks within applications.

syntax highlighting component for react with prismjs or highlightjs ast using inline styles

Pros of react-syntax-highlighter

  • Offers a wide range of syntax highlighting options for various programming languages
  • Provides multiple themes and styles out of the box
  • Lightweight and focused solely on syntax highlighting

Cons of react-syntax-highlighter

  • Lacks interactive code editing capabilities
  • Does not support live code execution or preview
  • Limited to static code display and highlighting

Code Comparison

react-syntax-highlighter:

import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';

const Component = () => {
  const codeString = '(num) => num + 1';
  return <SyntaxHighlighter language="javascript" style={docco}>{codeString}</SyntaxHighlighter>;
};

react-live:

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

const Component = () => (
  <LiveProvider code="() => <h1>Hello, World!</h1>">
    <LiveEditor />
    <LiveError />
    <LivePreview />
  </LiveProvider>
);

react-syntax-highlighter is ideal for displaying static code snippets with syntax highlighting, while react-live offers a more interactive experience with live code editing and preview capabilities. The choice between the two depends on whether you need simple code display or interactive code environments in your React application.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React Live — Formidable, We build the modern web

A flexible playground for live editing React code

Maintenance Status

React Live brings you the ability to render React components with editable source code and live preview.

The library is structured modularly and lets you style and compose its components freely.

Come learn more at our docs site!

Support

Have a question about react-live? Submit an issue in this repository using the "Question" template.

Notice something inaccurate or confusing? Feel free to open an issue or make a pull request to help improve the documentation for everyone!

The source for our docs site lives in this repo in the docs folder.

Contributing

Please see our contributing guide.

Maintenance Status

Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

NPM DownloadsLast 30 Days