What is react-live?
react-live is a React component that enables you to render live, editable code samples in your application. It provides a live code editor and preview, making it ideal for documentation, tutorials, and interactive coding environments.
What are react-live's main functionalities?
Live Code Editing
This feature allows users to edit code in a live editor and see the results immediately. The LiveProvider component wraps the code, and LiveEditor, LiveError, and LivePreview components handle the editing, error display, and preview respectively.
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
const code = `<strong>Hello World!</strong>`;
function App() {
return (
<LiveProvider code={code}>
<LiveEditor />
<LiveError />
<LivePreview />
</LiveProvider>
);
}
export default App;
Custom Scope
This feature allows you to provide custom components and variables to the live code editor. By passing a scope object to the LiveProvider, you can use external components and libraries in the live code.
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
import { Button } from 'my-ui-library';
const code = `<Button>Click me</Button>`;
const scope = { Button };
function App() {
return (
<LiveProvider code={code} scope={scope}>
<LiveEditor />
<LiveError />
<LivePreview />
</LiveProvider>
);
}
export default App;
Theming
This feature allows you to apply custom themes to the live code editor. By passing a theme object to the LiveProvider, you can style the code editor to match your application's design.
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
import { dracula } from 'react-syntax-highlighter/dist/esm/styles/prism';
const code = `<strong>Hello World!</strong>`;
function App() {
return (
<LiveProvider code={code} theme={dracula}>
<LiveEditor />
<LiveError />
<LivePreview />
</LiveProvider>
);
}
export default App;
Other packages similar to react-live
react-syntax-highlighter
react-syntax-highlighter is a syntax highlighting component for React using Prism or Highlight.js. Unlike react-live, it does not provide live editing or preview capabilities but focuses on syntax highlighting for static code snippets.
react-codemirror2
react-codemirror2 is a React component for CodeMirror, a versatile text editor implemented in JavaScript. It provides a rich code editing experience but does not include live preview functionality out of the box like react-live.
react-ace
react-ace is a React component for the Ace editor. It offers a powerful code editing experience with various features like syntax highlighting, themes, and keybindings. However, it does not provide live code preview capabilities like react-live.
React Live
A production-focused playground for live editing React code
React Live brings you the ability to render React components with editable source code and live preview.
It supports server-side rendering and comes in a tiny bundle.
The library is structured modularly and lets you style and compose its components freely.
Usage
Install it with npm install react-live
or yarn add react-live
and try out this piece of JSX:
import {
LiveProvider,
LiveEditor,
LiveError,
LivePreview
} from 'react-live'
<LiveProvider code="<strong>Hello World!</strong>">
<LiveEditor />
<LiveError />
<LivePreview />
</LiveProvider>
Demo
https://react-live.kitten.sh/
FAQ
How does it work?
It takes your code and transpiles it with Bublé, while the code is displayed using react-simple-code-editor
and the code is highlighted using prism-react-renderer
.
The transpiled code is then rendered in the preview component (LivePreview
), which does a fake mount if the code
is a React component.
What code can I use?
The code can be one of the following things:
- React elements, e.g.
<strong>Hello World!</strong>
- React pure functional components, e.g.
() => <strong>Hello World!</strong>
- React component classes
If you enable the noInline
prop on your LiveProvider
, you’ll be able to write imperative code,
and render one of the above things by calling render
.
How does the scope work?
The scope
prop on the LiveProvider
accepts additional globals. By default it injects React
only, which
means that the user can use it in their code like this:
class Example extends React.Component {
render() {
return <strong>Hello World!</strong>
}
}
But you can of course pass more things to the scope. They will be available as variables in the code. Here's an example using styled components:
import styled from 'styled-components';
const headerProps = { text: 'I\'m styled!' };
const scope = {styled, headerProps};
const code = `
const Header = styled.div\`
color: palevioletred;
font-size: 18px;
\`
render(<Header>{headerProps.text}</Header>)
`
<LiveProvider code={code} scope={scope} noInline={true}>
<LiveEditor />
<LiveError />
<LivePreview />
</LiveProvider>
API
<LiveProvider />
This component provides the context
for all the other ones. It also transpiles the user’s code!
It supports these props, while passing any others through to the children
:
Name | PropType | Description |
---|
code | PropTypes.string | The code that should be rendered, apart from the user’s edits |
scope | PropTypes.object | Accepts custom globals that the code can use |
noInline | PropTypes.bool | Doesn’t evaluate and mount the inline code (Default: false ) |
transformCode | PropTypes.func | Accepts and returns the code to be transpiled, affording an opportunity to first transform it |
language | PropTypes.string | What language you're writing for correct syntax highlighting. (Default: jsx ) |
disabled | PropTypes.bool | Disable editing on the <LiveEditor /> (Default: false ) |
theme | PropTypes.object | A prism-react-renderer theme object. See more here |
All subsequent components must be rendered inside a provider, since they communicate
using one.
The noInline
option kicks the Provider into a different mode, where you can write imperative-style
code and nothing gets evaluated and mounted automatically. Your example will need to call render
with valid JSX elements.
<LiveEditor />
This component renders the editor that displays the code. It is a wrapper around react-simple-code-editor
and the code highlighted using prism-react-renderer
.
<LiveError />
This component renders any error that occur while executing the code, or transpiling it.
It passes through any props to a pre
.
Note: Right now when the component unmounts, when there’s no error to be shown.
<LivePreview />
This component renders the actual component that the code generates inside an error boundary.
Name | PropType | Description |
---|
Component | PropTypes.node | Element that wraps the generated code. (Default: div ) |
withLive
The withLive
method creates a higher-order component, that injects the live-editing props provided
by LiveProvider
into a component.
Using this HOC allows you to add new components to react-live, or replace the default ones, with a new
desired behavior.
The component wrapped with withLive
gets injected the following props:
Name | Type | Description |
---|
code | string | Reflects the code that is passed in as the code prop |
error | string | An error that the code has thrown when it was previewed |
onError | function | A callback that, when called, changes the error to what's passed as the first argument |
onChange | function | A callback that accepts new code and transpiles it |
element | React.Element | The result of the transpiled code that is previewed |
Note: The code prop doesn't reflect the up-to-date code, but the code
prop, that is passed to the LiveProvider
.
FAQ
I want to use experimental feature x but Bublé doesn't support it! Can I use babel instead?
react-live
doesn't currently support configuring the transpiler and it ships with Bublé. The current workaround for using some experimental features bublé
doesn't support would be to use the transformCode
prop on LiveProvider
to transform your code with babel
alongside bublé
.
Here is a minimal example on how you could use babel
to support class-properties in react-live
:
There are multiple options when it comes to live, editable React component environments. Formidable actually has two first class projects to help you out: component-playground
and react-live
. Let's briefly look at the libraries, use cases, and factors that might help in deciding which is right for you.
Here's a high-level decision tree:
- If you want fast and easy setup and integration, then
component-playground
may be the ticket! - If you want a smaller bundle, SSR, and more flexibility, then
react-live
is for you!
Here are the various factors at play:
- Build:
component-playground
uses babel-standalone
, react-live
uses bublé
. (Note: react-live
might make transpiler customizable in the future). - Bundle size:
component-playground
has a larger bundle, but uses a more familiar editor setup. react-live
is smaller, but more customized editor around prism
. - Ease vs. flexibility:
react-live
is more modular/customizable, while component-playground
is easier/faster to set up. - SSR:
component-playground
is not server-side renderable, react-live
is. - Extra features:
component-playground
supports raw evaluation and pretty-printed output out-of-the-box, while react-live
does not. - Error handling:
component-playground
might have more predictable error handling than react-live
in some cases (due to react-dom
, although this might change with React 16).