# react-json-reconciler

> A reconciler for creating JSON objects from a react-jsx tree

Latest version **3.0.1** (published 2024-01-08) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2024-01-08 |
| First published | 2021-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 132.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 50 |
| Author | Adam Dierkens |
| Maintainers | adierkens |

## Links

- npm: https://www.npmjs.com/package/react-json-reconciler
- Repository: https://github.com/intuit/react-json-reconciler
- Homepage: https://github.com/intuit/react-json-reconciler#readme
- Issues: https://github.com/intuit/react-json-reconciler/issues
- npm.io page: https://npm.io/package/react-json-reconciler

## Dependencies (5)

- [source-map-js](https://npm.io/package/source-map-js.md) ^1.0.2
- [json-source-map](https://npm.io/package/json-source-map.md) ^0.6.1
- [react-reconciler](https://npm.io/package/react-reconciler.md) ^0.29.0
- [react-flatten-children](https://npm.io/package/react-flatten-children.md) ^1.1.2
- [@types/react-reconciler](https://npm.io/package/@types/react-reconciler.md) ^0.28.8

## Recent versions

- 3.0.1 (latest) — 2024-01-08
- 3.0.1--canary.11.230.0 (canary) — 2024-01-08
- 3.0.1--canary.11.224.0 — 2024-01-08
- 3.0.1--canary.11.218.0 — 2024-01-08
- 3.0.0 — 2023-12-21
- 3.0.0--canary.10.204.0 — 2023-12-19
- 3.0.0--canary.10.198.0 — 2023-12-19
- 2.0.0 — 2022-08-10
- 2.0.0--canary.7.139.0 — 2022-08-09
- 1.2.1--canary.133.6632047.0 — 2022-05-18
- 1.3.0--canary.6.127.0 — 2022-05-03
- 1.2.0 — 2022-03-23
- 1.2.0--canary.5.114.0 — 2022-03-23
- 1.2.0--canary.5.108.0 — 2022-03-22
- 1.1.1--canary.102.3938e94.0 — 2022-03-21
- … 8 more at https://npm.io/package/react-json-reconciler/versions

## README

<h1 align="center">
  <img width="400" alt="react-json-reconciler" src="./logo.png"/>
</h1>

This project leverages the `react-reconciler` to allow users to serialize JSX trees into JSON objects.

---

<div align="center">
<a href="https://circleci.com/gh/intuit/react-json-reconciler/tree/main"><img src="https://circleci.com/gh/intuit/react-json-reconciler/tree/main.svg?style=shield" alt="CircleCI" /></a>
<a href="https://www.npmjs.com/package/react-json-reconciler"><img src="https://img.shields.io/npm/v/react-json-reconciler.svg?style=flat-square&logo=npm" alt="npm" /></a> 
<a href="https://codecov.io/gh/intuit/react-json-reconciler">
  <img src="https://codecov.io/gh/intuit/react-json-reconciler/branch/main/graph/badge.svg?token=U4V5ZF55CO"/>
</a>
<a href="#contributors"><img src="https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square&logo=github" alt="All Contributors" /></a> 
<a href="https://github.com/prettier/prettier"><img src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square&logo=producthunt" alt="code style: prettier" /></a></div>

<br />

## Install

```
yarn add react-json-reconciler
npm i --save react-json-reconciler
```

## Usage

There are 4 primitive JSX elements, that can be used to construct a _normal_ JSX tree. All the things you'd expect to have access to in `React` (`state`, `hooks` `props`) all work as they normally would. To serialize the elements into JSON, call `render` and await the response. This will wait for all `useEffect` and `setState` updates to settle before returning the result.

```tsx
import React from "react";
import { render } from "react-json-reconciler";

const element = (
  <obj>
    <property key="Prop 1">
      <array>
        <value>Value 1</value>
      </array>
    </property>
    <property key="Prop 2">Value 2</property>
  </obj>
);

const { jsonValue } = await render(element);
```

The above will generate

```json
{
  "Prop 1": ["Value 1"],
  "Prop 2": "Value 2"
}
```

### Source Maps

In order to generate source-map support, `React` must be in _development_ mode (`process.env.NODE_ENV != 'production'`), and the [@babel/plugin-transform-react-jsx-source](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx-source) plugin enabled (included as part of `@babel/preset-react`)

Pass the `collectSourceMap: true` option to the `render` method.

### Refs

For each of the primitive types, any `ref` will return a respective JSON AST node. This allows users to introspect and manipulate the tree before rendering when coupled with a `useEffect()` or `useLayoutEffect()`.

Example:

```tsx
import React from "react";

const CustomComponent = (props) => {
  const objRef = React.useRef(null);

  React.useEffect(() => {
    // A chance to introspect the JSON AST node before being serialized
  }, [objRef]);

  return <obj ref={objRef}>{props.children}</obj>;
};
```

## License

`react-json-reconciler` is provided under the [MIT](./LICENSE) license.

## Contributors ✨

Feel free to open an issue or a pull request!

Make sure to read our [code of conduct](./CODE_OF_CONDUCT.md).

We actively welcome pull requests. Learn how to [contribute](./CONTRIBUTING.md).

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
  <tr>
    <td align="center"><a href="https://github.com/adierkens"><img src="https://avatars.githubusercontent.com/u/13004162?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Adam Dierkens</b></sub></a><br /><a href="https://github.com/intuit/react-json-reconciler/commits?author=adierkens" title="Code">💻</a> <a href="https://github.com/intuit/react-json-reconciler/commits?author=adierkens" title="Documentation">📖</a> <a href="#example-adierkens" title="Examples">💡</a> <a href="#infra-adierkens" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#maintenance-adierkens" title="Maintenance">🚧</a> <a href="#platform-adierkens" title="Packaging/porting to new platform">📦</a> <a href="https://github.com/intuit/react-json-reconciler/commits?author=adierkens" title="Tests">⚠️</a> <a href="#tool-adierkens" title="Tools">🔧</a></td>
    <td align="center"><a href="https://github.com/cliffordfajardo"><img src="https://avatars.githubusercontent.com/u/6743796?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Clifford Fajardo </b></sub></a><br /><a href="#infra-cliffordfajardo" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
  </tr>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

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