Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nedomas/react-code-sandbox

⚡️ React code sandbox renders React JSX source code from string to components.
https://github.com/nedomas/react-code-sandbox

jsx react reactjs sandbox

Last synced: 15 days ago
JSON representation

⚡️ React code sandbox renders React JSX source code from string to components.

Awesome Lists containing this project

README

        

# React code sandbox

React code sandbox renders React JSX source code from string to components. It
takes source as a string and returns renderable JSX code.

## Install

```sh
yarn add react-code-sandbox
# or
npm install react-code-sandbox --save
```

## Usage

```jsx
import CodeSandbox from 'react-code-sandbox'

import React from 'react'
import { Text } from 'candour'

const code = `
import { render } from 'react-dom'
import React from 'react'
import { Text } from 'candour'

render(


This is a source string

)
`

// add this to your app

{code}

```

## Render

`render` is a special global variable that is injected into the code parser.
When you call `render`, the source code of the first argument to it will be
returned from the sandbox.

This part is only used for display purposes and does not effect the source code.
```
import { render } from 'react-dom'
```

## Imports

Any imports that you specify in the source code have no actual effects and are
removed by the parser. They are replaced with variables injected by
the `imports` prop. When looking at the rendered code example,
it appears that libraries are imported, but they are just passed as local
variables under the scenes.

## How it works

It uses [babel](https://babeljs.io) in the browser to parse and convert the
source string into a usable JSX component. It will return parsed component
that was returned through `render` in the source code.

## Real world usage

React code sandbox is used throughout [Candour docs](https://candour.pro).