question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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'));

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
nsourovcommented, Oct 24, 2019
<ReactJson src={JSON.parse(JSON.stringify(json))}  />

This works well 😃

1reaction
Commandos87commented, Aug 6, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - JSON order mixed up - Stack Overflow
If you add this to your JSONObject then the resulting JSON would be in the same order as you have created it. import...
Read more >
react-json-view - npm
Name Type Default src JSON Object None name string or false "root" theme string "rjv‑default"
Read more >
Populating the tree using JSON - jsTree
The format remains the same as the above, the only difference is that the JSON is not inside the config object, but returned...
Read more >
Convert a JSON File to an Array in React - Pluralsight
Make sure that it has a string value coming from a server or the local source. If so, then the parse function will...
Read more >
Internals: JSON Output Format | Terraform
Terraform provides a machine-readable JSON representation of state, configuration and plan. ... e.g. "2.0" , for changes that are not backward-compatible.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found