- Notifications
You must be signed in to change notification settings - Fork24
Configure and build views using JSON schemas mapped to React components
License
TechniqueSoftware/react-json-schema
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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
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
constschema={"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"},...
When arrays of components exist (like children), react-json-schema will resolve a key for the element, which follows the rules fordynamic 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"},...
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 viasetComponentMap.
Example for exposing non-exposed components (ES6)
/* es6 object literal shorthand: { ContactForm } == { ContactForm: ContactForm } */contactForm.setComponentMap({ ContactForm, StringField});
UseparseSchema 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'));
importReactDOMfrom'react-dom';importReactJsonSchemafrom'react-json-schema';importFormStorefrom'FormStore';importCommentListfrom'CommentList';importCommentfrom'Comment';// For this example, let's pretend I already have data and am ignorant of actionsconstschema=FormStore.getFormSchema();constview=newReactJsonSchema();view.setComponentMap({ CommentList, Comment});ReactDOM.render(view.parseSchema(schema),document.getElementById('content'));
To run the demo
cd demo && npm installnpm start- The app will be served athttp://localhost:8080
If you'd like to ask a question, raise a concern, or contribute, please follow ourcontribution guidelines.
- 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.
- Playground on our public site for discoverability
- Possibility of react-native-json-schema
About
Configure and build views using JSON schemas mapped to React components
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.