Need help with json2react?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

txgruppi
158 Stars 8 Forks BSD 3-Clause "New" or "Revised" License 22 Commits 3 Opened issues

Description

Use JSON to create React Components.

Services available

!
?

Need anything else?

Contributors list

# 16,666
Dart
Go
React
nvm
21 commits

GitHub license David npm Travis Codecov

json2react

Use JSON to create React Stateless Components.

json2react
allows you to create React Stateless Components from JSON using a simple schema.

Why?

I needed a way to store static views on the database as data, not as HTML code.

Using this library you can fetch some remote data which represents an UI and render it with React.

Install

Like any other NPM package

npm install --save json2react

Usage

You can use it with:

  • React.render
  • As the return value, or part of it, of a stateless component
  • As the return value, or part of it, of a component's
    render
    method
import { createElement } from "react";
import j2r from "json2react";

const jsonUI = { type: "div", props: { style: { textAlign: "center" }, }, children: [ { type: "h1", children: "It works!" }, { type: "p", children: { type: "small", children: "This component was created from JSON", }, }, ], };

ReactDOM.render(j2r(createElement, jsonUI), document.body);

You can pass a mapper function as second argument to map types to components.

import { createElement } from "react";
import j2r from "json2react";
import MyDivComponent from "./MyDivComponent";

const jsonUI = { type: "MyDivComponent", props: { style: { textAlign: "center" }, }, children: [ { type: "h1", children: "It works!" }, { type: "p", children: { type: "small", children: "This component was created from JSON", }, }, ], };

const mapTypeToComponent = (type, props) => { switch (type) { case "MyDivComponent": return MyDivComponent; } return type; };

ReactDOM.render(j2r(createElement, mapTypeToComponent, jsonUI), document.body);

Schema

Please check the file http://github.com/txgruppi/json2react/blob/v0.0.0/schema.json for the detailed schema description.

Tests

Only tests

npm test

Tests and coverage

npm run coverage

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.