jsoneditor-react: Editor doesn't rerender when I change the 'value' props
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>`About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 3
- Comments: 26 (8 by maintainers)
@vankop thank you so much for your answer!
You can simply pass the
keyin the editor which makes it to re-render whenever the key values changes. I did like this:<Editor key={props.value.type} {...props}PS: Don’t pass object for
keysince it will be converted as[object Object]and remains same even if values change. In my codeprops.valueis an object andprops.value.typeis a string. so the editor re renders with new value for every value change, whereastypeis a unique value.@markdemich you need to get
refof<JsonEditor />to get access to editor instance explicitly. For instance:then you can do what ever you want with
jsonEditorusing apiI ended up rewriting this component myself using
jsoneditordirectly instead of depending onjsoneditor-react. It’s actually very simple. I’d like to share this but I’m not using Javascript, I’m using Scala and it looks like this:I also noticed you can avoid users of this lib to have to
import "jsoneditor/dist/jsoneditor.min.css";by adding the import in your component and configure your webpack as:Fix is found here https://github.com/vankop/jsoneditor-react/issues/4
<JsonEditor />is uncontrolled component, sovalueproperty is only initial value of json editor. If your task is to use both values in 2 editors you need explicitly get jsoneditors.For instance: