# react-json-view **Repository Path**: uiw/react-json-view ## Basic Information - **Project Name**: react-json-view - **Description**: 用于显示和编辑 javascript 数组和 JSON 对象的 React 组件。 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: main - **Homepage**: https://uiw.gitee.io/react-json-view - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-06-23 - **Last Updated**: 2023-06-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: React, TypeScript, JSON ## README react-json-view === [](https://github.com/uiwjs/react-json-view/actions/workflows/ci.yml) [](https://www.npmjs.com/package/@uiw/react-json-view) [](https://github.com/facebook/react/releases) [](https://uiwjs.github.io/react-json-view/lcov-report/) A React component for displaying and editing javascript arrays and JSON objects. ## Features 📚 Use Typescript to write, better code hints. 🎨 Support theme customization & [`online editing`](https://uiwjs.github.io/react-json-view/#online-editing-theme) theme 🌒 Support dark/light mode 📦 Zero dependencies ## Quick Start ```bash npm install @uiw/react-json-view ``` ```jsx import JsonView from '@uiw/react-json-view'; const avatar = 'https://i.imgur.com/MK3eW3As.jpg'; const longArray = new Array(1000).fill(1); const example = { avatar, string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, bigint: 10086n, null: null, undefined, timer: 0, date: new Date('Tue Sep 13 2022 14:07:44 GMT-0500 (Central Daylight Time)'), array: [19, 100.86, 'test', NaN, Infinity], nestedArray: [ [1, 2], [3, 4], ], object: { 'first-child': true, 'second-child': false, 'last-child': null, }, longArray, string_number: '1234', }; ``` ## Theme By default, the `lightTheme` light theme is used, and a `darkTheme` dark theme configuration is built in ```tsx mdx:preview import React from 'react'; import JsonView from '@uiw/react-json-view'; import { lightTheme } from '@uiw/react-json-view/light'; import { darkTheme } from '@uiw/react-json-view/dark'; const object = { string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, } export default function Demo() { return ( ) } ``` Example of custom `vscode` theme styles: ```tsx mdx:preview import React from 'react'; import JsonView from '@uiw/react-json-view'; const object = { string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, object: { 'first-child': true, 'second-child': false, 'last-child': null, }, } const customTheme = { '--w-rjv-font-family': 'monospace', '--w-rjv-color': '#9cdcfe', '--w-rjv-background-color': '#1e1e1e', '--w-rjv-line-color': '#323232', '--w-rjv-arrow-color': 'var(--w-rjv-color)', '--w-rjv-info-color': '#656565', '--w-rjv-copied-color': '#9cdcfe', '--w-rjv-copied-success-color': '#28a745', '--w-rjv-curlybraces-color': '#d4d4d4', '--w-rjv-brackets-color': '#d4d4d4', '--w-rjv-type-string-color': '#ce9178', '--w-rjv-type-int-color': '#268bd2', '--w-rjv-type-float-color': '#859900', '--w-rjv-type-bigint-color': '#268bd2', '--w-rjv-type-boolean-color': '#559bd4', '--w-rjv-type-date-color': '#586e75', '--w-rjv-type-null-color': '#d33682', '--w-rjv-type-nan-color': '#859900', '--w-rjv-type-undefined-color': '#586e75', }; export default function Demo() { return ( ) } ``` ## Online Editing Theme Online custom style example, please check in the [documentation website](https://uiwjs.github.io/react-json-view/) ```tsx mdx:preview:&title=Online Editing Theme import React, { useState, useRef } from 'react'; import Colorful from '@uiw/react-color-colorful'; import JsonView from '@uiw/react-json-view'; const object = { avatar: 'https://i.imgur.com/MK3eW3As.jpg', string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, bigint: 10086n, null: null, undefined, timer: 0, date: new Date('Tue Sep 13 2022 14:07:44 GMT-0500 (Central Daylight Time)'), array: [19, 100.86, 'test', NaN, Infinity], nestedArray: [ [1, 2], [3, 4], ], object: { 'first-child': true, 'second-child': false, 'last-child': null, }, string_number: '1234', } const customTheme = { '--w-rjv-color': '#9cdcfe', '--w-rjv-background-color': '#1e1e1e', '--w-rjv-line-color': '#323232', '--w-rjv-arrow-color': '#9cdcfe', '--w-rjv-info-color': '#656565', '--w-rjv-copied-color': '#0184a6', '--w-rjv-copied-success-color': '#28a745', '--w-rjv-curlybraces-color': '#d4d4d4', '--w-rjv-brackets-color': '#d4d4d4', '--w-rjv-type-string-color': '#ce9178', '--w-rjv-type-int-color': '#268bd2', '--w-rjv-type-float-color': '#859900', '--w-rjv-type-bigint-color': '#268bd2', '--w-rjv-type-boolean-color': '#559bd4', '--w-rjv-type-date-color': '#586e75', '--w-rjv-type-null-color': '#d33682', '--w-rjv-type-nan-color': '#859900', '--w-rjv-type-undefined-color': '#586e75', }; export default function Demo() { const [cssvar, setCssvar] = useState('--w-rjv-background-color'); const [hex, setHex] = useState("#1e1e1e"); const [theme, setTheme] = useState(customTheme); const onChange = ({ hexa }) => { setHex(hexa); setTheme({ ...theme, [cssvar]: hexa }); }; return ( {Object.keys(customTheme).map((varname, idx) => { const click = () => { setCssvar(varname); setHex(customTheme[varname]); }; const active = cssvar === varname ? '#a8a8a8' : ''; return {varname} })} Copy the theme configuration below into your project. {JSON.stringify(theme, null, 2)} ) } ``` ## Render ```tsx mdx:preview import React from 'react'; import JsonView from '@uiw/react-json-view'; const object = { string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, object: { 'first-child': true, 'second-child': false, 'last-child': null, }, } export default function Demo() { return ( , ellipsis: () => , objectKey: ({ value, ...props}) => { if (props.children === '"integer"' && value > 40) { return } return } }} /> ) } ``` **Preview Picture** ```tsx mdx:preview import React from 'react'; import JsonView from '@uiw/react-json-view'; const object = { avatar: 'https://i.imgur.com/MK3eW3As.jpg', string: 'Lorem ipsum dolor sit amet', integer: 42, } function value({ type, children, ...props }) { if (type === 'string' && /\.(jpg)$/.test(children)) { return ( ); } } export default function Demo() { return ( ) } ``` ## Modify Icon Style Use built-in default icons. ```tsx mdx:preview import React from 'react'; import JsonView from '@uiw/react-json-view'; import { TriangleArrow } from '@uiw/react-json-view/triangle-arrow'; import { TriangleSolidArrow } from '@uiw/react-json-view/triangle-solid-arrow'; const object = { string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, object: { 'first-child': true, 'second-child': false, 'last-child': null, }, nestedArray: [ [1, 2], [3, 4], ], } export default function Demo() { return ( }} /> ) } ``` Display of custom **svg** `icon` components ```tsx mdx:preview import React from 'react'; import JsonView from '@uiw/react-json-view'; const object = { string: 'Lorem ipsum dolor sit amet', integer: 42, float: 114.514, object: { 'first-child': true, 'second-child': false, 'last-child': null, }, nestedArray: [ [1, 2], [3, 4], ], } const Arrow = (props) => { const { style, 'data-expand': expand, ...reset } = props; const svgProps = { style: { cursor: 'pointer', height: '1em', width: '1em' }, viewBox: '0 0 24 24', fill: 'var(--w-rjv-arrow-color, currentColor)', ...reset } if (!expand) { return ( ); } return ( ); } export default function Demo() { return ( }} /> ) } ``` ## Props ```ts import React from 'react'; import { MetaProps, SemicolonProps, EllipsisProps, ValueViewProps } from '@uiw/react-json-view'; export interface JsonViewProps extends React.DetailedHTMLProps, HTMLDivElement> { /** This property contains your input JSON */ value?: T; /** Set the indent-width for nested objects @default 15 */ indentWidth?: number; /** When set to `true`, data type labels prefix values @default true */ displayDataTypes?: boolean; /** When set to `true`, `objects` and `arrays` are labeled with size @default true */ displayObjectSize?: boolean; /** Define the root node name. @default undefined */ keyName?: string | number; /** The user can copy objects and arrays to clipboard by clicking on the clipboard icon. @default true */ enableClipboard?: boolean; /** Display for quotes in object-key @default " */ quotes?: "'" | '"' | ''; /** When set to true, all nodes will be collapsed by default. Use an integer value to collapse at a particular depth. @default false */ collapsed?: boolean | number; /** Callback function for when a treeNode is expanded or collapsed */ onExpand?: (props: { expand: boolean; value: T; keyid: string; keyName?: string | number; }) => void; /** Redefine interface elements to re-render. */ components?: { braces?: MetaProps['render']; ellipsis?: EllipsisProps['render']; arrow?: JSX.Element; objectKey?: SemicolonProps['render']; value?: ValueViewProps['renderValue']; }; } declare const JsonView: React.ForwardRefExoticComponent, "ref"> & React.RefAttributes>; export default JsonView; ``` ## Size and dependencies Here is the size benchmark (using [bundlephobia.com](https://bundlephobia.com)) against similar React libraries (found by [`npmjs.com/search`](https://www.npmjs.com/search?q=react%20json&ranking=popularity)): | Library | Bundle size | Bundle size (gzip) | Deps | Last commit | | ------- | ------- | ------- | ------- | ------- | | **@uiw/react-json-view** | [](https://bundlephobia.com/result?p=@uiw/react-json-view) | [](https://bundlephobia.com/result?p=@uiw/react-json-view) | [](https://bundlephobia.com/result?p=@uiw/react-json-view) | [](https://github.com/uiwjs/react-json-view/commits) | | react-json-view-lite | [](https://bundlephobia.com/result?p=react-json-view-lite) | [](https://bundlephobia.com/result?p=react-json-view-lite) | [](https://bundlephobia.com/result?p=react-json-view-lite) | [](https://github.com/AnyRoad/react-json-view-lite/commits) | | react-json-pretty | [](https://bundlephobia.com/result?p=react-json-pretty) | [](https://bundlephobia.com/result?p=react-json-pretty) | [](https://bundlephobia.com/result?p=react-json-pretty) | [](https://github.com/chenckang/react-json-pretty/commits) | | react-json-inspector | [](https://bundlephobia.com/result?p=react-json-inspector) | [](https://bundlephobia.com/result?p=react-json-inspector) | [](https://bundlephobia.com/result?p=react-json-inspector) | [](https://github.com/Lapple/react-json-inspector/commits) | | react-json-tree | [](https://bundlephobia.com/result?p=react-json-tree) | [](https://bundlephobia.com/result?p=react-json-tree) | [](https://bundlephobia.com/result?p=react-json-tree) | [](https://github.com/reduxjs/redux-devtools/commits) | | react-json-view | [](https://bundlephobia.com/result?p=react-json-view) | [](https://bundlephobia.com/result?p=react-json-view) | [](https://bundlephobia.com/result?p=react-json-view) | [](https://github.com/mac-s-g/react-json-view/commits) | | react-json-tree-viewer | [](https://bundlephobia.com/result?p=react-json-tree-viewer) | [](https://bundlephobia.com/result?p=react-json-tree-viewer) | [](https://bundlephobia.com/result?p=react-json-tree-viewer) | [](https://github.com/nsisodiya/react-json-viewer/commits) | | react-domify | [](https://bundlephobia.com/result?p=react-domify) | [](https://bundlephobia.com/result?p=react-domify) | [](https://bundlephobia.com/result?p=react-domify) | [](https://github.com/JedWatson/react-domify/commits) | | react18-json-view | [](https://bundlephobia.com/result?p=react18-json-view) | [](https://bundlephobia.com/result?p=react18-json-view) | [](https://bundlephobia.com/result?p=react18-json-view) | [](https://github.com/YYsuni/react18-json-view/commits) | | @textea/json-viewer | [](https://bundlephobia.com/result?p=@textea/json-viewer) | [](https://bundlephobia.com/result?p=@textea/json-viewer) | [](https://bundlephobia.com/result?p=@textea/json-viewer) | [](https://github.com/TexteaInc/json-viewer/commits) | ## Development Runs the project in development mode. ```bash # Step 1, run first, listen to the component compile and output the .js file # listen for compilation output type .d.ts file npm run watch # Step 2, development mode, listen to compile preview website instance npm run start ``` Builds the app for production to the build folder. ```bash npm run build ``` The build is minified and the filenames include the hashes. Your app is ready to be deployed! ## Contributors As always, thanks to our amazing contributors! Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors). ## License Licensed under the MIT License.
{JSON.stringify(theme, null, 2)}