json2react

0.2.4 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i json2react

Weekly Downloads

1,072

Version

0.2.4

License

GPLv3

Unpacked Size

15.5 kB

Total Files

10

Last publish

Collaborators

  • txgruppi