Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/txgruppi/json2react
Use JSON to create React Components.
https://github.com/txgruppi/json2react
json react schema ui
Last synced: 3 months ago
JSON representation
Use JSON to create React Components.
- Host: GitHub
- URL: https://github.com/txgruppi/json2react
- Owner: txgruppi
- License: bsd-3-clause
- Created: 2016-04-03T02:59:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:40:29.000Z (almost 2 years ago)
- Last Synced: 2024-03-25T13:22:15.145Z (8 months ago)
- Topics: json, react, schema, ui
- Language: JavaScript
- Homepage:
- Size: 142 KB
- Stars: 167
- Watchers: 4
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-json - json2react - Use JSON to create React Stateless Components. (Libraries)
README
[![GitHub license](https://img.shields.io/github/license/txgruppi/json2react.svg?style=flat-square)](https://github.com/txgruppi/json2react)
[![David](https://img.shields.io/david/txgruppi/json2react.svg?style=flat-square)](https://github.com/txgruppi/json2react)
[![npm](https://img.shields.io/npm/v/json2react.svg?style=flat-square)](https://www.npmjs.com/package/json2react)
[![Travis](https://img.shields.io/travis/txgruppi/json2react.svg?style=flat-square)](https://travis-ci.org/txgruppi/json2react)
[![Codecov](https://img.shields.io/codecov/c/github/txgruppi/json2react.svg?style=flat-square)](https://codecov.io/github/txgruppi/json2react)# 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
```sh
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```javascript
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.
```javascript
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
```sh
npm test
```Tests and coverage
```sh
npm run coverage
```