1.4.0 • Published 3 days ago

react-json-view-lite v1.4.0

Weekly downloads
24
License
MIT
Repository
github
Last release
3 days ago

Install

npm install --save react-json-view-lite

Migration from the 0.9.x versions

  1. Property shouldInitiallyExpand has different name shouldExpandNode in order to emphasize that it will be called every time properties change.
  2. If you use custom styles:
    • pointer and expander are no longer used
    • component uses collapseIcon, expandIcon, collapsedContent styles in order to customize expand/collapse icon and collpased content placeholder which were previously hardcode to the , and .... Default style values use ::after pseudo-classes to set the content.

Usage

import * as React from 'react';

import { JsonView, allExpanded, darkStyles, defaultStyles } from 'react-json-view-lite';
import 'react-json-view-lite/dist/index.css';

const json = {
  a: 1,
  b: 'example'
};

const App = () => {
  return (
    <React.Fragment>
      <JsonView data={json} shouldExpandNode={allExpanded} style={defaultStyles} />
      <JsonView data={json} shouldExpandNode={allExpanded} style={darkStyles} />
    </React.Fragment>
  );
};

export default App;

Please note that in JavaScript, an anonymous function like function() {} or () => {} always creates a different function every time component is rendered, so you might need to use useCallback React Hook for the shouldExpandNode parameter or extract the function outside the functional component.

StoryBook

https://anyroad.github.io/react-json-view-lite/

Props

NameTypeDefault ValueDescription
dataObject | Array<any>Data which should be rendered
styleStylePropsdefaultStylesOptional. CSS classes for rendering. Library provides two build-in implementations: darkStyles, defaultStyles (see below)
shouldExpandNode(level: number, value: any, field?: string) => booleanallExpandedOptional. Function which will be called during initial rendering for each Object and Array of the data in order to calculate should if this node be expanded. Note that this function will be called again to update the each node state once the property value changed. level startes from 0, field does not have a value for the array element. Library provides two build-in implementations: allExpanded and collapseAllNested (see below)
clickToExpandNodebooleanfalseOptional. Set to true if you want to expand/collapse nodes by clicking on the node itself.

Extra exported

NameTypeDescription
defaultStylesStylePropsDefault styles for light background
darkStylesStylePropsDefault styles for dark background
allExpanded() => booleanAlways returns true
collapseAllNested(level: number) => booleanReturns true only for the first level (level=0)

StyleProps

NameTypeDescription
containerstringCSS class name for rendering parent block
basicChildStylestringCSS class name for property block containing property name and value
collapseIconstringCSS class name for rendering button collapsing Object and Array nodes. Default content is .
expandIconstringCSS class name for rendering button expanding Object and Array nodes. Default content is .
collapsedContentstringCSS class name for rendering placeholder when Object and Array nodes are collapsed. Default contents is ....
labelstringCSS class name for rendering property names
clickableLabelstringCSS class name for rendering clickable property names (requires the clickToExpandNode prop to be true)
nullValuestringCSS class name for rendering null values
undefinedValuestringCSS class name for rendering undefined values
numberValuestringCSS class name for rendering numeric values
stringValuestringCSS class name for rendering string values
booleanValuestringCSS class name for rendering boolean values
otherValuestringCSS class name for rendering all other values except Object, Arrray, null, undefined, numeric, boolean and string
punctuationstringCSS class name for rendering ,, [, ], {, }
noQuotesForStringValuesbooleanwhether or not to add double quotes when rendering string values, default value is false

Comparison with other libraries

Size and dependencies

Here is the size benchmark (using bundlephobia.com) against similar React libraries (found by https://www.npmjs.com/search?q=react%20json&ranking=popularity):

LibraryBundle sizeBundle size (gzip)Dependencies
react-json-view-litenpm.ionpm.ionpm.io
react-json-prettynpm.ionpm.ionpm.io
react-json-inspectornpm.ionpm.ionpm.io
react-json-treenpm.ionpm.ionpm.io
react-json-viewnpm.ionpm.ionpm.io
react-json-tree-viewernpm.ionpm.ionpm.io

Performance

Performance was mesaured using the react-component-benchmark library. Every component was rendered 50 times using the 300Kb json file as data source, please refer to source code of the benchmark project. All numbers are in milliseconds. Tests were performed on Macbook Air M1 16Gb RAM usging Chrome v96.0.4664.110(official build, arm64). Every component was tested 2 times but there was no significant differences in the results.

LibraryMinMaxAverageMedianP90
react-json-view-lite8160419582582
react-json-pretty2259322456
react-json-inspector6821 109758711905
react-json-tree5651 217658620741
react-json-view1 4031 72215291 5401 631
react-json-tree-viewer266663320278455

As you can see react-json-pretty renders faster than other libraries but it does not have ability to collapse/expand nested objects so it might be good choice if you need just json syntax highlighting.

License

MIT © AnyRoad