My JSON React
Transform a plain javascript object or a json string in a React component.
Basically transform this:
{
"component": "HelloWorld",
"props": {
"text": {
"value": "hello world!!!"
}
},
"children": [
{
"component": "Text",
"props": {
"text": {
"value": "text 1"
}
}
},
{
"component": "NumberText",
"props": {
"text": {
"value": "text 2"
},
"number": {
"value": 1
}
}
}
]
}
in this:
<HelloWorld text="hello world!!!">
<Text text="text 1" key={ 1 }/>
<NumberText text="text 2" number={ 1 } key={ 2 }/>
</HelloWorld>
Install
$ npm install --save my-json-react
Usage
import myJsonReact from 'my-json-react'
import jsonObj from '../my.json'
const components {
}
const jsonRendered = myJsonReact(jsonObj, components)
return <div>{ jsonRendered }</div>
myJsonReact(jsonData, components, options)
{object|string} jsonData
Javascript object or json string with the data they will be used to create the React component.
Json format
{
"component": "CompnentName"
"props": {
"propName": {
"value": "prop value",
"type": "type prop"
},
},
"children": {
"component": "ComponentName"
"props": {}
"children": {
}
}
}
-
component (string)
: Is the name of you react component. This should be defined previously in the object component of the function.
-
props (object optional)
: Object with the component properties. Each object key is the property name and the object value is another object with:
value
Property value.- `type (string optional). Property type. You can define and use custom types. See typeManager option.
-
children (object|array optional)
- If is a object, this must replicate the main structure again (
component
, props
, children
) - If is an array, each element of the array must be an object with the main structure (
component
, props
, children
)
{Object} components
List with all components that you wan use in the json files. The object keys is the component name inside of the json file. The object values is the react component
{Object} Options
-
errorComponent (ReactComponent)
React component to customize the possible exceptions generate by the function. This error component has one property exception
, that has the exception object.
-
typeManager (TypeManager)
Object to define the custom type for the properties.
Example
const jsonObject = {
'component': 'ComponentGrandFather',
'props': {
'propName1': {
'value': 'prop value 1'
}
},
'children': {
'component': 'ComponentFather',
'props': {
'propName2': {
'value': 'prop value 2'
}
}
'children': [
{
'component': 'Child',
'props': { 'propName3': 'child 1' }
},
{
'component': 'Child',
'props': { 'propName3': 'child 2' }
}
]
}
}
const componentsList = {
ComponentGrandFather, ComponentFather, Child
}
const ErrorComponent = props => (
<div>
<h1>Error</h1>
<p>{ props.exception.message }</p>
</div>
)
const options = { errorComponent: ErrorComponent }
const jsonRendered = start(jsonObject, componentList, options);
class TypeManager
You can define new property types using this class.
It is usefull to customize the react component properties.
How define new types. Example
import myJsonReact, { TypeManager } from 'my-json-react'
const DummyComponent = props => <div>{ props.name }</div>
class CustomTypeManager extends TypeManager {
toLower(value) {
return value.toLowerCase();
}
dummyComponent(value) {
return <DummyComponent name={ value }/>
}
}
const jsonObject = {
'component': 'ComponentTest1'
'props': {
'propName1': {
'value': 'HELLO WORLD',
'type': 'toLower'
},
'propName2': {
'value': 'name 1',
'type': 'dummyComponent'
}
}
}
const listComponents = { ComponentTest1 }
const options = { typeManager: new CustomTypeManager() }
const componentRendered = myJsonReact(jsonObject, listComponents, options)
LICENSE
This project is licensed under the terms of the MIT license.