Skip to content

Commit d4e02d7

Browse files
committed
Avoid unnecessary state update when setting focus
1 parent 9709f5a commit d4e02d7

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

src/pages/index.js

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ class Index extends React.Component {
7474

7575
componentDidMount() {
7676
document.onblur = () => {
77-
this.setState({
78-
focusedPane: null,
79-
});
77+
this.setFocus(null);
8078
}
8179
}
8280

@@ -141,18 +139,14 @@ class Index extends React.Component {
141139
}
142140

143141
handleInsertButtonClick = () => {
144-
this.setState({
145-
focusedPane: this.state.insertPanelsAreOpen ? null : 'InsertPanels',
146-
});
142+
this.setFocusIf('insertPanelsAreOpen', null, 'InsertPanels')
147143
this.setPersistentState({
148144
insertPanelsAreOpen: !this.state.insertPanelsAreOpen,
149145
});
150146
}
151147

152148
handleNodeFormatButtonClick = () => {
153-
this.setState({
154-
focusedPane: this.state.nodeFormatDrawerIsOpen ? null: 'NodeFormatDrawer',
155-
});
149+
this.setFocusIf('nodeFormatDrawerIsOpen', null, 'NodeFormatDrawer')
156150
this.setPersistentState({
157151
nodeFormatDrawerIsOpen: !this.state.nodeFormatDrawerIsOpen,
158152
edgeFormatDrawerIsOpen: false,
@@ -163,15 +157,11 @@ class Index extends React.Component {
163157
this.setPersistentState({
164158
nodeFormatDrawerIsOpen: false,
165159
});
166-
this.setState({
167-
focusedPane: null,
168-
});
160+
this.setFocus(null);
169161
}
170162

171163
handleEdgeFormatButtonClick = () => {
172-
this.setState({
173-
focusedPane: this.state.edgeFormatDrawerIsOpen ? null: 'EdgeFormatDrawer',
174-
});
164+
this.setFocusIf('edgeFormatDrawerIsOpen', null, 'EdgeFormatDrawer')
175165
this.setPersistentState({
176166
edgeFormatDrawerIsOpen: !this.state.edgeFormatDrawerIsOpen,
177167
nodeFormatDrawerIsOpen: false,
@@ -182,9 +172,7 @@ class Index extends React.Component {
182172
this.setPersistentState({
183173
edgeFormatDrawerIsOpen: false,
184174
});
185-
this.setState({
186-
focusedPane: null,
187-
});
175+
this.setFocus(null);
188176
}
189177

190178
handleSettingsClick = () => {
@@ -408,44 +396,48 @@ class Index extends React.Component {
408396
}
409397

410398
handleTextEditorFocus = () => {
411-
this.setState({
412-
focusedPane: 'TextEditor',
413-
});
399+
this.setFocus('TextEditor');
414400
}
415401

416402
handleTextEditorBlur = () => {
417-
if (this.state.focusedPane === 'TextEditor') {
418-
this.setState({
419-
focusedPane: null,
420-
});
421-
}
403+
this.setFocusIfFocusIs('TextEditor', null);
422404
}
423405

424406
handleGraphFocus = () => {
425-
this.setState({
426-
focusedPane: 'Graph',
427-
});
407+
this.setFocus('Graph');
428408
}
429409

430410
handleInsertPanelsClick = () => {
431-
this.setState({
432-
focusedPane: 'InsertPanels',
433-
});
411+
this.setFocus('InsertPanels');
434412
}
435413

436414
handleNodeFormatDrawerClick = () => {
437-
this.setState((state) => {
438-
return {
439-
focusedPane: state.nodeFormatDrawerIsOpen ? 'NodeFormatDrawer' : null,
440-
}
441-
});
415+
this.setFocusIf('nodeFormatDrawerIsOpen', 'NodeFormatDrawer', null)
442416
}
443417

444418
handleEdgeFormatDrawerClick = () => {
419+
this.setFocus('EdgeFormatDrawer');
420+
this.setFocusIf('edgeFormatDrawerIsOpen', 'EdgeFormatDrawer', null)
421+
}
422+
423+
setFocus = (focusedPane) => {
424+
this.setState((state) => (state.focusedPane !== focusedPane && {
425+
focusedPane: focusedPane,
426+
}) || null);
427+
}
428+
429+
setFocusIfFocusIs = (currentlyFocusedPane, newFocusedPane) => {
430+
this.setState((state) => (state.focusedPane === currentlyFocusedPane && {
431+
focusedPane: newFocusedPane,
432+
}) || null);
433+
}
434+
435+
setFocusIf = (stateProperty, focusedPaneIf, focusedPaneElse) => {
445436
this.setState((state) => {
446-
return {
447-
focusedPane: state.edgeFormatDrawerIsOpen ? 'EdgeFormatDrawer' : null,
448-
}
437+
const focusedPane = state[stateProperty] ? focusedPaneIf: focusedPaneElse;
438+
return (state.focusedPane !== focusedPane && {
439+
focusedPane: focusedPane,
440+
}) || null;
449441
});
450442
}
451443

0 commit comments

Comments
 (0)