{"id":25972151,"url":"https://github.com/mainframeos/react-json-renderer","last_synced_at":"2025-03-05T00:19:48.259Z","repository":{"id":57333512,"uuid":"91123910","full_name":"MainframeOS/react-json-renderer","owner":"MainframeOS","description":null,"archived":false,"fork":false,"pushed_at":"2018-04-03T10:13:28.000Z","size":167,"stargazers_count":37,"open_issues_count":4,"forks_count":3,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-02-19T21:08:19.488Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MainframeOS.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-12T19:35:54.000Z","updated_at":"2023-04-18T07:35:46.000Z","dependencies_parsed_at":"2022-08-24T19:01:35.894Z","dependency_job_id":null,"html_url":"https://github.com/MainframeOS/react-json-renderer","commit_stats":null,"previous_names":["mainframehq/react-json-renderer"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MainframeOS%2Freact-json-renderer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MainframeOS%2Freact-json-renderer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MainframeOS%2Freact-json-renderer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MainframeOS%2Freact-json-renderer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MainframeOS","download_url":"https://codeload.github.com/MainframeOS/react-json-renderer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241941199,"owners_count":20046026,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-05T00:19:47.602Z","updated_at":"2025-03-05T00:19:48.252Z","avatar_url":"https://github.com/MainframeOS.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-json-renderer\n\nBasic converter of a React component tree to a JS object or JSON string and associated renderer using provided components.\n\n## Installation\n\n```sh\nyarn add react-json-renderer # Yarn\nnpm install react-json-renderer # npm\n```\n\nReact is a required peer dependency, make sure to install it as well if you haven't already:\n\n```sh\nyarn add react # Yarn\nnpm install react # npm\n```\n\n## APIs\n\n### convertToObject\n\n```js\nconvertToObject(\n  tree: React.Element\u003cany\u003e,\n  params?: {\n    processMeta?: (tree: React.Element\u003cany\u003e) =\u003e ({\n      name: string,\n      type: 'function' | 'string' | 'unknown',\n    }),\n    processProps?: (props: Object) =\u003e Object,\n  },\n): Object\n```\n\nConverts a component tree to a formatted Object supported by the `renderFromObject()` function.\n\n### convertToJSON\n\n```js\nconvertToJSON(\n  tree: React.Element\u003c*\u003e,\n  params?: {\n    processMeta?: (tree: React.Element\u003cany\u003e) =\u003e ({\n      name: string,\n      type: 'function' | 'string' | 'unknown',\n    }),\n    processProps?: (props: Object) =\u003e Object,\n    space?: number | string,\n  },\n): Object\n```\n\nConverts a component tree to a JSON string supported by the `Renderer` component.\n\n### renderFromObject\n\n```js\nrenderFromObject(\n  tree: Object,\n  params?: {\n    components?: {\n      [type: string]: React.ComponentType\u003cany\u003e,\n    },\n    fallback?: React.ComponentType\u003cany\u003e,\n  },\n): React.Element\u003c*\u003e\n```\n\nConverts a formatted Object to a React elements tree ready to be rendered by React.\n\n## Components\n\n### Renderer\n\n**Props:**\n\n- `json?: string`: Formatted Object tree in JSON.\n- `tree?: Object`: Formatted Object tree.\n\n## Example\n\n**Server or other source**\n\n```js\nimport React from 'react'\nimport { convertToJSON } from 'react-json-renderer'\n\n// Simple strings will be exported as component types and functions will be executed\nconst Text = 'Text'\nconst View = 'View'\nconst Welcome = ({ name }) =\u003e\n  \u003cView\u003e\n    \u003cText\u003eWelcome {name}!\u003c/Text\u003e\n  \u003c/View\u003e\n\n// Returns the JSON payload to provide to the client\nexport const createWelcome = (name) =\u003e convertToJSON(\u003cWelcome name={name} /\u003e)\n```\n\n**Web client**\n\n```js\nimport React from 'react'\nimport { Renderer } from 'react-json-renderer'\n\n// Components from the payload supported by the client\nconst components = {\n  Text: ({ children }) =\u003e \u003cspan className='text'\u003e{children}\u003c/span\u003e,\n  View: ({ children }) =\u003e \u003cdiv className='view'\u003e{children}\u003c/div\u003e,\n}\n\n// Fallback for unsupported components, if not provided it will return null and therefore not render the component and its children\nconst Fallback = ({ children }) =\u003e \u003cdiv className='fallback'\u003e{children}\u003c/div\u003e\n\n// Inject the JSON payload from the server and render with provided component and fallback\nexport const PayloadRenderer = ({ payload }) =\u003e\n  \u003cRenderer components={components} fallback={Fallback} json={payload} /\u003e\n```\n\n**React-Native client**\n\n```js\nimport React from 'react'\nimport { Renderer } from 'react-json-renderer'\nimport { StyleSheet, Text, View } from 'react-native'\n\nconst styles = StyleSheet.create({\n  text: {\n    fontSize: 14,\n  },\n  view: {\n    flex: 1,\n  },\n})\n\nconst RenderText = ({ children }) =\u003e \u003cText style={styles.text}\u003e{children}\u003c/Text\u003e\nconst RenderView = ({ children }) =\u003e \u003cView style={styles.view}\u003e{children}\u003c/View\u003e\nconst components = {\n  Text: RenderText,\n  View: RenderView,\n}\n\nexport const PayloadRenderer = ({ payload }) =\u003e\n  \u003cRenderer components={components} fallback={RenderView} json={payload} /\u003e\n```\n\n## License\n\nMIT  \nSee [LICENSE](LICENSE) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmainframeos%2Freact-json-renderer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmainframeos%2Freact-json-renderer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmainframeos%2Freact-json-renderer/lists"}