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

react-json-schema

v1.2.2

Published

Write component schema in JSON; parse to create react elements.

Downloads

829

Readme

react-json-schema

npm install react-json-schema

Construct React elements from JSON by mapping JSON definitions to React components. Use react-json-schema for data-driven layouts, or as an abstraction layer for React components and props.

Render anywhere (as long as it's DOM)! Since react-json-schema does not perform any rendering, the method in which you want to render is up to you. For example, you can use ReactDOMServer.render, ReactDOM.renderToString, etc. if you'd like. This also means JSX is not a dependency for react-json-schema.

Quick Documentation and Examples

Full Documentation

Schema

The primary resource needed is a defined schema in JSON or a JavaScript object literal. It's recommended that schema attributes mainly define React component props. The parser explicitly handles the following attributes:

  • component: MUST exist and be defined by a string or React component (must be a string if describing a native HTML tag)
  • children: MAY exist to define sub-components
  • text: MAY exist to as a string to define inner HTML text (overrides children)
  • key: MAY exist to define a key for dynamic children

Example JSON schema

const schema = {
  "component": "CommentList",
  "children": [
    {
      "component": "Comment",
      "author": "Pete Hunt",
      "children": "This is one comment"
    },
    {
      "component": "Comment",
      "author": "Jordan Walke",
      "children": "This is *another* comment"
    },
    {
      "component": "a",
      "href": "#help",
      "text": "I need help"
    }
  ]
};

Example JS literal

...
  {
    "component": Comment,
    "author": "Pete Hunt",
    "children": "This is one comment"
  },
...
Dynamic Children and Keys

When arrays of components exist (like children), react-json-schema will resolve a key for the element, which follows the rules for dynamic children. It will either use a custom key if defined, or resolve a numeric key based on the array index.

Example of defining child keys

...
  {
    "component": "Comment",
    "key": "0ab19f8e", // defined key
    "author": "Pete Hunt",
    "children": "This is one comment"
  },
...

Component Mapping

React components need to be exposed to the react-json-schema so that the parser can create React elements. If the schema contains object literals with component references, the schema is exposing the React components and no additional configuration is needed. If the schema does not contain references to components, the components can be exposed via setComponentMap.

Example for exposing non-exposed components (ES6)

/* es6 object literal shorthand: { ContactForm } == { ContactForm: ContactForm } */
contactForm.setComponentMap({ ContactForm, StringField });

Parsing

Use parseSchema to render React elements. It returns the root node. Note that if your schema's root is an array, you'll have to wrap the schema in an element.

Example (ES6)

ReactDOM.render(contactForm.parseSchema(schema),
  document.getElementById('contact-form'));

Complete Example

import ReactDOM from 'react-dom';
import ReactJsonSchema from 'react-json-schema';

import FormStore from 'FormStore';
import CommentList from 'CommentList';
import Comment from 'Comment';

// For this example, let's pretend I already have data and am ignorant of actions
const schema = FormStore.getFormSchema();
const view = new ReactJsonSchema();

view.setComponentMap({ CommentList, Comment });

ReactDOM.render(view.parseSchema(schema),
  document.getElementById('content'));

Demo an Example Codebase

To run the demo

  • cd demo && npm install
  • npm start
  • The app will be served at http://localhost:8080

Contribution and Code of Conduct

If you'd like to ask a question, raise a concern, or contribute, please follow our contribution guidelines.

Alternatives

  • react-jsonschema-form: A React component for building Web forms from JSON Schema. This library further abstracts React components, making it easier to build forms. Also, it comes with components. React-json-schema is a lighter alternative that allows the use of any components.

Roadmap

  • Playground on our public site for discoverability
  • Possibility of react-native-json-schema