-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor doesn't rerender when I change the 'value' props #3
Comments
For instance: class YourComponent extends React.Component {
set1EditorRef = instance => this.editor1 = instance;
set2EditorRef = instance => this.editor2 = instance;
1editorChangeHandler = jsonData => {
this.setState({ jsonData });
this.editor2.jsonEditor.set(jsonData);
};
2editorChangeHandler = jsonData => {
this.setState({ jsonData });
this.editor1.jsonEditor.set(jsonData);
};
render() {
return [
<Editor
ref={this.set1EditorRef}
value={this.state.jsonData}
ajv={ajv}
onChange={this.1editorChangeHandler}
search={false}
navigationBar={false}
/>
<Editor
ref={this.set2EditorRef}
value={this.state.jsonData}
onChange={this.2editorChangeHandler}
mode="code"
/>
];
}
} |
The main problem with controlled state is lossing focus in |
Thank you @vankop |
I have a similar problem, but a little different. I have one editor that I want to change it's contents based on a listbox selection. Currently, there doesn't seem to be a way to do that. |
@markdemich you need to get 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 |
I have a little different error. I have 2 tabs, and one contains default tree with json, the second has |
@OlegRyzhkov do you using |
I exactly made it and it worked, but only if I render both at the same time. also, it rerender if I wrap one of the editor with div. looks like react can't know when to rerender element. |
also, prop onChange doesn't work on json with error. how to know when user type something? |
better to control jsoneditor instance by yourself. Regarding to this example #3 (comment).
|
it could rerender if
|
Editor wrapper updates only on https://github.com/vankop/jsoneditor-react/blob/master/src/Editor.jsx#L176 |
This component should really be doing this #3 (comment) itself. |
@joan38 It is possible, but in this case it will require a lot more maintenance =( |
I ended up rewriting this component myself using import "jsoneditor/dist/jsoneditor.min.css";
object JsonEditor {
val component = FunctionalComponent[Props] { props =>
val classes = useStyles()
val (editor, updateEditor) = useState(null)
val editorRef = React.createRef[HTMLLabelElement]
useEffect(
() =>
updateEditor(
new JSONEditor(
editorRef.current,
{
mode: props.mode,
modes: props.modes.map(_.toJSArray),
onChange: props.onChange,
onChangeText: props.onChangeText
// TODO Add all the options here
},
JSON.parse(props.value)
)
),
Seq.empty
)
useEffect(() => if (editor != null) editor.updateText(props.value), Seq(props.value))
div(ref := editorRef, className := classes.editor)
}
} I also noticed you can avoid users of this lib to have to
|
@vankop thank you so much for your answer!
|
re- rendering single editor my code is |
Thanks @uooopss Just a few minor tweaks to work as pretty much a dropin module. File: JSONEditor.js
File: App.js
|
@hutch120 Thanks for sharing your solution. |
You can simply pass the
PS: Don't pass object for |
@sridharan21994 can you elaborate on your fix here? I have the issue where as you edit the value, with each keystroke, the editor is re-rendered which makes it impossible to edit more than a single character at a time. I am trying to use the approach @hutch120 pasted above. |
Fix is found here #4 |
@hutch120, I really appreciate your solution above. I've tried replicating it in Typescript. But unfortunately, I'm stuck with this error
|
@hanoak You need to add a proper interface:
Hope helps! |
@xochilpili When I implement the above interface i get a new error What should be the correct interface. Thanks in advance |
Leaving this here with two purposes; (1) for future googlers, (2) to give people here the opportunity to tell my why this is a bad idea 😅. Here we go: <Editor value={myData} key={JSON.stringify(myData)} />
What am I missing? I should probably clarify that my use-case is 100% read-only. No actual editing needed. If you need editing and need to persist user input, you probably need to control whether or not the |
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:`
<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"
/>
The text was updated successfully, but these errors were encountered: