# json-react

> Convert objects to React elements and elements to serializable objects

Latest version **1.0.0** (published 2018-02-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-react
pnpm add json-react
yarn add json-react
bun add json-react
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-02-20 |
| First published | 2018-02-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Brent Jackson |
| Maintainers | jxnblk |

## Links

- npm: https://www.npmjs.com/package/json-react
- npm.io page: https://npm.io/package/json-react

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^16.2.0

## Recent versions

- 1.0.0 (latest) — 2018-02-20

## README

# json-react

Convert objects to React elements and elements to serializable objects

```sh
npm i json-react
```

Convert React element to object

```js
import jsonReact from 'json-react'

const el = (
  <div>
    <h1>Hello</h1>
  </div>
)

const obj = jsonReact.toObject(el)
```

```js
// returns
{
  type: 'div',
  props: null,
  children: [
    {
      type: 'h1',
      props: null,
      children: [
        'Hello'
      ]
    }
  ]
}
```

Convert object to React element

```js
import jsonReact from 'json-react'

const el = jsonReact.toElement({
  type: 'div',
  props: null,
  children: [
    {
      type: 'h1',
      props: null,
      children: [
        'Hello'
      ]
    }
  ]
})
```

```js
// returns
<div>
  <h1>Hello</h1>
</div>
```

Convert object to React element with references to components

```js
import jsonReact from 'json-react'
import MyComponent from './MyComponent'

const el = jsonReact.toElement({
  type: 'MyComponent',
  props: {},
  children: [
    'Hello'
  ]
}, { MyComponent })

// <MyComponent>Hello</MyComponent>
```

## Why?

- Demonstrate how React elements are objects and can be converted to and from JSON
- Components can be serialized by displayName
- Components can be passed as scope to create elements from objects

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