react-json-view icon indicating copy to clipboard operation
react-json-view copied to clipboard

RJV does not reflect back the json in tree when json state is changed

Open kamesh95 opened this issue 5 years ago • 6 comments

I am using React JSON View to dynamically update the source object linked with the tree. However I am not able to do so because it doesn't take into account the changes in the object through state change. Hence, I see the old json even though new keys are added or removed. Here's a small example showing the behaviour, what I want here is that whenever the setInterval calls the add function and adds a key to the json object, it should show that key in the tree as well.

Note: When I collapse and expand the tree again, it does show the updated json.

import React from 'react';
import ReactDOM from 'react-dom';
import ReactJsonView from 'react-json-view'

class JSONViewer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {json: {
      Name: 'Kamesh',
      Age: 23,
      Array: [1, 2, 3]
    }};
  }
  
  render() {
    return (
      <div>
        <ReactJsonView src={this.state.json}/>
      </div>
    );
  }

  componentDidMount() {
   setInterval(
      () => {
        let x = Math.random();
        this.add(Object.assign(this.state.json, {
          [x]: 'RandomKey' 
        }))
      },
      3000
    );
  }

  add(newJSON) {
    this.setState({
      json: newJSON
    });
  }
}

ReactDOM.render(<JSONViewer />, document.getElementById('root'));

kamesh95 avatar Mar 05 '19 16:03 kamesh95

I am also running into this limitation... great component but I need to be able to show updates to the json made externally!

elijahsmith avatar May 14 '19 11:05 elijahsmith

me too

CasperMXP avatar Jun 03 '19 07:06 CasperMXP

I encountered the same problem and could solve it by parsing the JSON string to an actual JSON object:

This did'nt work: <ReactJson src={this.state.label} theme="monokai"/>

This works: <ReactJson src={JSON.parse(this.state.label)} theme="monokai"/>

Best regards, Sven

Commandos87 avatar Aug 06 '19 15:08 Commandos87

<ReactJson src={JSON.parse(JSON.stringify(json))}  />

This works well :smiley:

nsourov avatar Oct 24 '19 08:10 nsourov

why isn't anyone fixing this?

supercmmetry avatar Nov 29 '21 08:11 supercmmetry

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

CasperMXP avatar Dec 20 '23 19:12 CasperMXP