The React parent component rendering process automatically compresses rendering of child components. It can be a performance boost for most of application, but situation like this can be a serious problem.
I have 2 components :
- File list sidebar component
- Text editor component (I'm using Slate for now)
Two component is child component of Home component.
When user clicks the sidebar item, it should :
- Highlight the selected item
- Set a new editor state by reading the text file
Second step can be a bottleneck. The problem here is, that the React stupidly compresses the seperated two operations altogether. This causes the user can't see item highlight right after click! User Must wait for reading the file, and set a new editor state, and then, finally, item can get highlighted.
This delay, is causing badly at user experience. Did you ever used a text editor that reacts your click about ~500ms after? The program looks "slower".
componentDidUpdate(prevProps) {
console.log('componentDidUpdate!');
const { currentDir, actions, currentFile } = this.props;
if (prevProps.currentDir !== currentDir) {
updatedFileList(currentDir);
} else if (prevProps.currentFile !== currentFile) {
updateEditorContent(currentFile);
}
}
This is the componentDidUpdate. This updates editor state.
redux-logger.js:1 action SELECT_FILE_LIST_ITEM @ 21:29:18.332
21:29:18.338 redux-logger.js:1 action UPDATE_CURRENT_FILE @ 21:29:18.338
21:29:18.347 Home.js:287 render!!
21:29:18.356 Home.js:44 componentDidUpdate!
21:29:18.366 redux-logger.js:1 action UPDATE_EDITOR_CONTENT @ 21:29:18.357
21:29:18.373 Home.js:287 render!!
21:29:18.678 Home.js:44 componentDidUpdate!
The log says it componentDidUpdate
d at 21:29:18.356
, but actual HTML DOM is not updated yet. It acutally updates DOM after 21:29:18.678 Home.js:44 componentDidUpdate!
.'
How to seperate two componentDidUpdates, to update DOM seperately?