npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

my-json-react

v0.0.2

Published

Transform a plain javascript object or json string in a react component.

Downloads

7

Readme

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 {
  // list of react components defined in json obj
}

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
{
  // React component name,
  "component": "CompnentName"
   // Object with the component props. *Optional
  "props": {
    // prop name
    "propName": {
      // prop value
      "value": "prop value",
       // prop type. *optional
      "type": "type prop"
    },
    // more props...
  },
  // List of the children of the component. Children property can be:
  // - An object with another component
  // - An array with a list of object with other components
  "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

// javascript plain object with react structure.
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' }
      }
    ]
  }
}

// list of components used in json object.
const componentsList = {
  ComponentGrandFather, ComponentFather, Child
}

// define a custom error component.
const ErrorComponent = props => (
  <div>
    <h1>Error</h1>
    <p>{ props.exception.message }</p>
  </div>
)

const options = { errorComponent: ErrorComponent }

const jsonRendered = start(jsonObject, componentList, options);
/* the react component rendered is:

<ComponentGrandFather propName1="prop value 1">
  <ComponentFather propName2="prop value 2">
    <Child propName3="child 1" key={ 1 }/>
    <Child propName3="child 2" key={ 2 }/>
  </ComponentFather>
</ComponentGrandFather>

*/

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>

// make custom class with the types.
class CustomTypeManager extends TypeManager {
  // make methods with the type names.
  // dummy example.
  toLower(value) {
    return value.toLowerCase();
  }

  dummyComponent(value) {
    return <DummyComponent name={ value }/>
  }
}

const jsonObject = {
  'component': 'ComponentTest1'
  'props': {
    'propName1': {
      'value': 'HELLO WORLD',
      // PropName1 is of type toLower. Defined in CustomTypeManager
      'type': 'toLower'
    },
    'propName2': {
      'value': 'name 1',
      // PropName1 is of type dummyComponent. Defined in CustomTypeManager
      'type': 'dummyComponent'
    }
  }
}

const listComponents = { ComponentTest1 }
const options = { typeManager: new CustomTypeManager() }

const componentRendered = myJsonReact(jsonObject, listComponents, options)
/*
component rendered:
<ComponentTest1 propName1="hello world" propName2={ <DummyComponent name="name 1"/> }/>
*/

LICENSE

This project is licensed under the terms of the MIT license.