# string-to-react

> convert string to react component

Latest version **1.0.2** (published 2019-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install string-to-react
pnpm add string-to-react
yarn add string-to-react
bun add string-to-react
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-04-11 |
| First published | 2019-04-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 605.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | PorUnaCabeza |
| Maintainers | makpia |
| Keywords | react, converter, parser, html |

## Links

- npm: https://www.npmjs.com/package/string-to-react
- Repository: https://github.com/PorUnaCabeza/string-to-react
- Homepage: https://github.com/PorUnaCabeza/string-to-react#readme
- Issues: https://github.com/PorUnaCabeza/string-to-react/issues
- npm.io page: https://npm.io/package/string-to-react

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-04-11
- 1.0.1 — 2019-04-11
- 1.0.0 — 2019-04-11

## README

## String to React

[![Build Status](https://travis-ci.org/PorUnaCabeza/string-to-react.svg?branch=master)](https://travis-ci.org/PorUnaCabeza/string-to-react) [![Coverage Status](https://coveralls.io/repos/github/PorUnaCabeza/string-to-react/badge.svg)](https://coveralls.io/github/PorUnaCabeza/string-to-react) [![npm](https://img.shields.io/npm/v/string-to-react.svg)](https://www.npmjs.com/package/string-to-react)

A tool to convert JSX strings into React Components

### Installation:

```
npm install string-to-react --save-dev
```

### Usage:

```javascript
import ReactDOM from 'react-dom'
import StringToReact from 'string-to-react'
let s = `<div>hi</div>`
ReactDOM.render(StringToReact(s), document.getElementById('container'))
```

### Customized Component

```javascript
import React from 'react'
import ReactDOM from 'react-dom'
import StringToReact from 'string-to-react'

class View extends React.Component {
  render() {
    return <div {...this.props}>{this.props.children}</div>
  }
}

let s = `<View style={{fontSize: '12px'}}>hi</View>`

let transform = function(tagName) {
  if (tagName === 'View') return View
  return null
}

ReactDOM.render(StringToReact(s, transform), document.getElementById('container'))
```

### Api

#### `function StringToReact(string, [transform])`

- `string`: The JSX string
- `transform`: `Function`
  - input: `tagName:string`
  - output: `FunctionComponent | ComponentClass | string | null`
  - typescript declare:
    ```typescript
    interface Transform {
      (tagName: string): FunctionComponent | ComponentClass | string | null
    }
    ```
  - example:
    ```javascript
    import View from './View.jsx'
    let transform = function(tagName) {
      if (tagName === 'pdiv') return 'div'
      else if (tagName === 'View') return View
      return null  // must return null if no match
    }
    ```

---
_Source: https://npm.io/package/string-to-react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
