Update how placeholder property triggers a component re-render.
See original GitHub issue- What version of RJEA are you using (react-json-editor-ajrm version)? 2.5.5
- What operating system and processor architecture are you using? macOS 10.13.6
- What did you do? Update placeholder passed to RJEA through onChange
- What did you expect to see? Cursor position is maintained
- What did you see instead? Cursor position is reset to 0
import React, { Component } from 'react';
import JSONInput from 'react-json-editor-ajrm';
import locale from 'react-json-editor-ajrm/locale/en';
class App extends Component {
constructor(props){
super(props);
this.state = {
obj: {name: 'Bob'},
};
this.objUpdate = this.objUpdate.bind(this);
}
objUpdate(arg){
if(arg.jsObject){
this.setState({obj: arg.jsObject});
}
}
render() {
return (
<JSONInput
id = 'a_unique_id'
placeholder = { this.state.obj }
locale = { locale }
onChange = { this.objUpdate }
/>
);
}
}
export default App;
This is my App.js after create-react-app and adding RJEA. I’m passing obj
which belongs to App’s state as placeholder. Whenever I trigger an onChange (use the arrow keys, make edits) the state is set again.
Just moving the cursor around and waiting for an update (1 sec) causes the cursor to jump to start.
Moving the this.getCursorPosition() above the onChange() partially fixes the error. The cursor no longer jumps to the start, but when the cursor is at the start of a line it jumps to the end of the previous line.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:11 (6 by maintainers)
Top Results From Across the Web
When does React re-render components? - Felix Gerschau
Want to see re-rendering in action? React DevTools lets you highlight renders under Components -> View Settings -> Highlight updates when ...
Read more >How and when to force a React component to re-render
Generally, forcing a React component re-render isn't best practice, even when React fails to update the components automatically.
Read more >than needed React components re-rendering when typing in ...
I understand that setSearchInput will re-render SearchBar component whenever I type in it, but I don't get why CompanyList re-renders. Search ...
Read more >What Causes a React Component to Re-Render - YouTube
In this video, I will go over what causes a react component to re-render.The base reason a component re-renders is because the props...
Read more >Dynamically Change State-Bound Content on Successful API ...
Elements of the rendered content of a React component can be bound against properties of the component's state . Whenever we update the ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @cgvedant,
Your observation was brilliant. I’ve taken your feedback into account. I’ve extended the logic to how changes in
placeholder
determine whether component will re-render:If the same value for the
placeholder
property is provided, the component it will not re-render. However, if the component’s content has already been edited and differs from its initialplaceholder
value, sending the sameplaceholder
value will effectively allow the component to re-render, thus resetting it.Please let me know if this works for you.
@AndrewRedican I see a potential issue with the way showPlaceholder updates. Suppose I’m editing a JSON with a reset button that resets the content to some template. Previously for this type of reset to work, we could just pass the same template JSON but as a new object. But now we would need to actually change the content of the template or make an intermediate change for the reset to work. This is not how I’m going to use this component, but it could be a very confusing gotcha for people who don’t know how this component treats placeholder.
I feel the best way to go forward is to let react handle the diffing of props and always accept the latest value from the placeholder. Again, adding a flag that ignores placeholder after mount looks like the best design to me(I’m very new to react though).