Only update if structure of JSON changes

This commit is contained in:
Lukas Martinelli 2016-12-29 21:49:40 +01:00
parent ae9afdd8d9
commit 3be3a716d4

View file

@ -32,12 +32,24 @@ class JSONEditor extends React.Component {
}) })
} }
shouldComponentUpdate(nextProps, nextState) {
try {
const parsedLayer = JSON.parse(this.state.code)
// If the structure is still the same do not update
// because it affects editing experience by reformatting all the time
return nextState.code !== JSON.stringify(parsedLayer, null, 2)
} catch(err) {
return true
}
}
onCodeUpdate(newCode) { onCodeUpdate(newCode) {
try { try {
const parsedLayer = JSON.parse(newCode) const parsedLayer = JSON.parse(newCode)
this.props.onChange(parsedLayer) this.props.onChange(parsedLayer)
} catch(err) { } catch(err) {
console.warn(err) console.warn(err)
} finally {
this.setState({ this.setState({
code: newCode code: newCode
}) })