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.

Editor doesn't rerender when I change the 'value' props

See original GitHub issue

I am using two editors at same time. An update in one editor should update the second one and viceversa. I have used same state variable in both editors and i have handled onChange in both editors by changing the state variable jsonData. Still when I change JSON data in one editor the other one doesn’t rerender. It remains same. The following is my code:

`<div> <Editor value={this.state.jsonData} ajv={ajv} onChange={ (jsonData) => this.setState({ jsonData }) } search={false} navigationBar={false} /> <Editor value={this.state.jsonData} onChange={ (jsonData) => this.setState({ jsonData }) } mode=“code” />

</div>`

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
uooopsscommented, Dec 14, 2020

@vankop thank you so much for your answer!

const JsonEditor = ({ data = {}, handleJSON}) => {
  const jsonEditorRef = useRef(null)

  useEffect(() => {
    if (jsonEditorRef.current !== null) {
      jsonEditorRef.current.set(data);
    }
  }, [data])

  const setRef = instance => {
    if (instance) {
      jsonEditorRef.current = instance.jsonEditor;
    } else {
      jsonEditorRef.current = null;
    }
  };

  return (
    <Editor
      ref={setRef}
      value={data}
      onChange={handleJSON}
      search={false}
      statusBar={true}
      mode='code'
      htmlElementProps={{
        style: {
          height: 500
        },
      }}
    />
  );
} 
4reactions
vankopcommented, Nov 29, 2018

@markdemich you need to get ref of <JsonEditor /> to get access to editor instance explicitly. For instance:

class YourComponent extends React.Component {
   setRef = instance => {
         if (instance) {
                const {jsonEditor} = instance;
               this.jsonEditor = jsonEditor;
         } else {
               this.jsonEditor = null;
         }
   };

  render() {
     return (
          <Editor
                     ref={this.setRef}
             />
    );
  }
}

then you can do what ever you want with jsonEditor using api

Read more comments on GitHub >

github_iconTop Results From Across the Web

React prop value not updating in object initialization during re ...
1 Answer 1 ; export const ; { onChange, leaseDocument, save, rentTableData }: any) => { // rentTableData is the prop that will...
Read more >
When does React re-render components? - Felix Gerschau
this.props.user.name = 'Felix'; Don't do this! Instead of changing the props like this, you need to change the state in the parent component. ......
Read more >
Hooks FAQ - React
Do Hooks replace render props and higher-order components? What do Hooks mean for ... How to read an often-changing value from useCallback? Under...
Read more >
Re-rendering Components in ReactJS - GeeksforGeeks
A second or subsequent render to update the state is called as re-rendering. React components automatically re-render whenever there is a change ......
Read more >
Change state without Re-rendering in React - Anshul Jain
In React, whenever the Props or State of the component change, ... to re-render where useRef doesn't re-render on updating the values.
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