React Runner

Run your React code on the go

README

React Runner


Run your React code on the go https://react-runner.vercel.app

Features


- Inline element
- Function component
- Class component, with class fields support
- Composing components with render or export default
- Server Side Rendering
- import statement
- Typescript

With React Runner, you can write your live code in the real world way, check out Hacker News in react-runner vs in real world, with the same code

You can even build your own async runner to support dynamic imports, try Play React

Install


  1. ``` sh
  2. # Yarn
  3. yarn add react-runner

  4. # NPM
  5. npm install --save react-runner
  6. ```

Options


- code string, _required_ the code to be ran
- scope object globals that could be used in code

Usage


  1. ``` js
  2. import { Runner } from 'react-runner'

  3. const element = <Runner code={code} scope={scope} onRendered={handleRendered} />
  4. ```

or use hook useRunner with cache support

  1. ``` js
  2. import { useRunner } from 'react-runner'

  3. const { element, error } = useRunner({ code, scope })
  4. ```

import statement and multi files


  1. ``` js
  2. import { importCode } from 'react-runner'
  3. import * as YourPkg from 'your-pkg'

  4. const baseScope = {
  5.   /* base globals */
  6. }

  7. const scope = {
  8.   ...baseScope,
  9.   // scope used by import statement
  10.   import: {
  11.     constants: { A: 'a' },
  12.     'your-pkg': YourPkg,
  13.     './local-file': importCode(localFileContent, baseScope),
  14.   },
  15. }
  16. ```

then in your live code you can import them

  1. ``` js
  2. import { A } from 'constants'
  3. import Foo, { Bar } from 'your-pkg'
  4. import What, { Ever } from './local-file'

  5. export default function Demo() {
  6.   /* render */
  7. }
  8. ```

Browser support


  1. ```
  2. "browserslist": [
  3.   "Chrome > 61",
  4.   "Edge > 16",
  5.   "Firefox > 60",
  6.   "Safari > 10.1"
  7. ]
  8. ```

Resources



react-live-runner


react-runner is inspired by react-live heavily,
I love it, but I love arrow functions for event handlers instead of bind them manually as well as other modern features,
and I don't want to change my code to be compliant with restrictions, so I created this project,
use Sucrase instead of Bublé to transpile the code.

If you are using react-live in your project and want a smooth transition, react-live-runner is there for you which provide the identical way to play with, and react-live-runner re-exports react-runner so you can use everything in react-runner by importing react-live-runner

  1. ``` js
  2. import {
  3.   LiveProvider,
  4.   LiveEditor,
  5.   LiveError,
  6.   LivePreview,
  7. } from 'react-live-runner'

  8. ...
  9. <LiveProvider code={code} scope={scope}>
  10.   <LiveEditor />
  11.   <LivePreview />
  12.   <LiveError />
  13. </LiveProvider>
  14. ...
  15. ```

or hooks for better custom rendering

  1. ``` js
  2. import { useLiveRunner, CodeEditor } from 'react-live-runner'

  3. const { element, error, code, onChange } = useLiveRunner({
  4.   initialCode,
  5.   scope,
  6.   transformCode,
  7. })

  8. ...
  9. <>
  10.   <CodeEditor value={code} onChange={onChange}>
  11.   <div>{element}</div>
  12.   {error && <pre>{error}</pre>}
  13. </>
  14. ...
  15. ```

or use react-runner directly

  1. ``` js
  2. import { useState, useEffect } from 'react'
  3. import { useRunner } from 'react-runner'

  4. const [code, onChange] = useState(initialCode)
  5. const { element, error } = useRunner({ code, scope })

  6. useEffect(() => {
  7.   onChange(initialCode)
  8. }, [initialCode])

  9. ...
  10. <>
  11.   <textarea value={code} onChange={event => onChange(event.target.value)}>
  12.   <div>{element}</div>
  13.   {error && <pre>{error}</pre>}
  14. </>
  15. ...
  16. ```

Check the real world usage here https://github.com/nihgwu/react-runner/blob/master/website/src/components/LiveRunner.tsx

License


MIT © Neo Nie