ESLint: Re-enable rule no-access-state-in-setstate (#10870)

* Re-enable rule no-access-state-in-setstate

* Move accessing event values out of async functions
This commit is contained in:
Kamil Gabryjelski 2020-09-21 08:48:23 +02:00 committed by GitHub
parent 3d8f757ac8
commit 4835d3b0a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 83 additions and 71 deletions

View File

@ -124,7 +124,6 @@ module.exports = {
'react/jsx-fragments': 1, 'react/jsx-fragments': 1,
'react/jsx-no-bind': 0, 'react/jsx-no-bind': 0,
'react/jsx-props-no-spreading': 0, // re-enable up for discussion 'react/jsx-props-no-spreading': 0, // re-enable up for discussion
'react/no-access-state-in-setstate': 0, // disabled temporarily
'react/no-array-index-key': 0, 'react/no-array-index-key': 0,
'react/no-string-refs': 0, 'react/no-string-refs': 0,
'react/no-unescaped-entities': 0, 'react/no-unescaped-entities': 0,
@ -236,7 +235,6 @@ module.exports = {
'react/jsx-fragments': 1, 'react/jsx-fragments': 1,
'react/jsx-no-bind': 0, 'react/jsx-no-bind': 0,
'react/jsx-props-no-spreading': 0, // re-enable up for discussion 'react/jsx-props-no-spreading': 0, // re-enable up for discussion
'react/no-access-state-in-setstate': 0, // disabled temporarily
'react/no-array-index-key': 0, 'react/no-array-index-key': 0,
'react/no-string-refs': 0, 'react/no-string-refs': 0,
'react/no-unescaped-entities': 0, 'react/no-unescaped-entities': 0,

View File

@ -150,12 +150,12 @@ export default class CRUDCollection extends React.PureComponent<
toggleExpand(id: any) { toggleExpand(id: any) {
this.onCellChange(id, '__expanded', false); this.onCellChange(id, '__expanded', false);
this.setState({ this.setState(prevState => ({
expandedColumns: { expandedColumns: {
...this.state.expandedColumns, ...prevState.expandedColumns,
[id]: !this.state.expandedColumns[id], [id]: !prevState.expandedColumns[id],
}, },
}); }));
} }
renderHeaderRow() { renderHeaderRow() {

View File

@ -77,7 +77,7 @@ export default class LimitControl extends React.PureComponent<
} }
handleToggle() { handleToggle() {
this.setState({ showOverlay: !this.state.showOverlay }); this.setState(prevState => ({ showOverlay: !prevState.showOverlay }));
} }
handleHide() { handleHide() {

View File

@ -129,9 +129,9 @@ export default class ResultSet extends React.PureComponent<
} }
toggleExploreResultsButton() { toggleExploreResultsButton() {
this.setState({ this.setState(prevState => ({
showExploreResultsButton: !this.state.showExploreResultsButton, showExploreResultsButton: !prevState.showExploreResultsButton,
}); }));
} }
changeSearch(event: React.ChangeEvent<HTMLInputElement>) { changeSearch(event: React.ChangeEvent<HTMLInputElement>) {

View File

@ -91,7 +91,7 @@ class SaveQuery extends React.PureComponent {
} }
toggleSave() { toggleSave() {
this.setState({ showSave: !this.state.showSave }); this.setState(prevState => ({ showSave: !prevState.showSave }));
} }
renderModalBody() { renderModalBody() {

View File

@ -133,7 +133,7 @@ class ScheduleQueryButton extends React.PureComponent {
} }
toggleSchedule() { toggleSchedule() {
this.setState({ showSchedule: !this.state.showSchedule }); this.setState(prevState => ({ showSchedule: !prevState.showSchedule }));
} }
renderModalBody() { renderModalBody() {

View File

@ -260,7 +260,9 @@ class SqlEditor extends React.PureComponent {
} }
handleToggleAutocompleteEnabled = () => { handleToggleAutocompleteEnabled = () => {
this.setState({ autocompleteEnabled: !this.state.autocompleteEnabled }); this.setState(prevState => ({
autocompleteEnabled: !prevState.autocompleteEnabled,
}));
}; };
handleWindowResize() { handleWindowResize() {

View File

@ -278,7 +278,7 @@ class TabbedSqlEditors extends React.PureComponent {
} }
toggleLeftBar() { toggleLeftBar() {
this.setState({ hideLeftBar: !this.state.hideLeftBar }); this.setState(prevState => ({ hideLeftBar: !prevState.hideLeftBar }));
} }
render() { render() {

View File

@ -85,7 +85,7 @@ class TableElement extends React.PureComponent {
} }
toggleSortColumns() { toggleSortColumns() {
this.setState({ sortColumns: !this.state.sortColumns }); this.setState(prevState => ({ sortColumns: !prevState.sortColumns }));
} }
removeFromStore() { removeFromStore() {

View File

@ -70,7 +70,7 @@ class OmniContainer extends React.Component {
show_omni: !this.state.showOmni, show_omni: !this.state.showOmni,
}); });
this.setState({ showOmni: !this.state.showOmni }); this.setState(prevState => ({ showOmni: !prevState.showOmni }));
document.getElementsByClassName('Omnibar')[0].focus(); document.getElementsByClassName('Omnibar')[0].focus();
} }

View File

@ -75,7 +75,9 @@ class SaveModal extends React.PureComponent {
} }
toggleDuplicateSlices() { toggleDuplicateSlices() {
this.setState({ duplicateSlices: !this.state.duplicateSlices }); this.setState(prevState => ({
duplicateSlices: !prevState.duplicateSlices,
}));
} }
handleSaveTypeChange(event) { handleSaveTypeChange(event) {

View File

@ -135,23 +135,23 @@ class SliceAdder extends React.Component {
} }
searchUpdated(searchTerm) { searchUpdated(searchTerm) {
this.setState({ this.setState(prevState => ({
searchTerm, searchTerm,
filteredSlices: this.getFilteredSortedSlices( filteredSlices: this.getFilteredSortedSlices(
searchTerm, searchTerm,
this.state.sortBy, prevState.sortBy,
), ),
}); }));
} }
handleSelect(sortBy) { handleSelect(sortBy) {
this.setState({ this.setState(prevState => ({
sortBy, sortBy,
filteredSlices: this.getFilteredSortedSlices( filteredSlices: this.getFilteredSortedSlices(
this.state.searchTerm, prevState.searchTerm,
sortBy, sortBy,
), ),
}); }));
} }
rowRenderer({ key, index, style }) { rowRenderer({ key, index, style }) {

View File

@ -103,9 +103,9 @@ class SliceHeaderControls extends React.PureComponent {
} }
toggleControls() { toggleControls() {
this.setState({ this.setState(prevState => ({
showControls: !this.state.showControls, showControls: !prevState.showControls,
}); }));
} }
handleToggleFullSize() { handleToggleFullSize() {

View File

@ -163,7 +163,7 @@ class ChartHolder extends React.Component {
} }
handleToggleFullSize() { handleToggleFullSize() {
this.setState(() => ({ isFullSize: !this.state.isFullSize })); this.setState(prevState => ({ isFullSize: !prevState.isFullSize }));
} }
render() { render() {

View File

@ -289,7 +289,10 @@ class DatasourceEditor extends React.PureComponent {
onDatasourcePropChange(attr, value) { onDatasourcePropChange(attr, value) {
const datasource = { ...this.state.datasource, [attr]: value }; const datasource = { ...this.state.datasource, [attr]: value };
this.setState({ datasource }, this.onDatasourceChange(datasource)); this.setState(
prevState => ({ datasource: { ...prevState.datasource, [attr]: value } }),
this.onDatasourceChange(datasource),
);
} }
setColumns(obj) { setColumns(obj) {

View File

@ -88,40 +88,41 @@ export default class AdhocMetricEditPopover extends React.Component {
} }
onColumnChange(column) { onColumnChange(column) {
this.setState({ this.setState(prevState => ({
adhocMetric: this.state.adhocMetric.duplicateWith({ adhocMetric: prevState.adhocMetric.duplicateWith({
column, column,
expressionType: EXPRESSION_TYPES.SIMPLE, expressionType: EXPRESSION_TYPES.SIMPLE,
}), }),
}); }));
} }
onAggregateChange(aggregate) { onAggregateChange(aggregate) {
// we construct this object explicitly to overwrite the value in the case aggregate is null // we construct this object explicitly to overwrite the value in the case aggregate is null
this.setState({ this.setState(prevState => ({
adhocMetric: this.state.adhocMetric.duplicateWith({ adhocMetric: prevState.adhocMetric.duplicateWith({
aggregate, aggregate,
expressionType: EXPRESSION_TYPES.SIMPLE, expressionType: EXPRESSION_TYPES.SIMPLE,
}), }),
}); }));
} }
onSqlExpressionChange(sqlExpression) { onSqlExpressionChange(sqlExpression) {
this.setState({ this.setState(prevState => ({
adhocMetric: this.state.adhocMetric.duplicateWith({ adhocMetric: prevState.adhocMetric.duplicateWith({
sqlExpression, sqlExpression,
expressionType: EXPRESSION_TYPES.SQL, expressionType: EXPRESSION_TYPES.SQL,
}), }),
}); }));
} }
onLabelChange(e) { onLabelChange(e) {
this.setState({ const label = e.target.value;
adhocMetric: this.state.adhocMetric.duplicateWith({ this.setState(prevState => ({
label: e.target.value, adhocMetric: prevState.adhocMetric.duplicateWith({
label,
hasCustomLabel: true, hasCustomLabel: true,
}), }),
}); }));
} }
onDragDown(e) { onDragDown(e) {

View File

@ -44,7 +44,7 @@ export default class ControlPanelSection extends React.Component {
} }
toggleExpand() { toggleExpand() {
this.setState({ expanded: !this.state.expanded }); this.setState(prevState => ({ expanded: !prevState.expanded }));
} }
renderHeader() { renderHeader() {

View File

@ -281,7 +281,7 @@ class ExploreViewContainer extends React.Component {
} }
toggleModal() { toggleModal() {
this.setState({ showModal: !this.state.showModal }); this.setState(prevState => ({ showModal: !prevState.showModal }));
} }
hasErrors() { hasErrors() {

View File

@ -333,7 +333,7 @@ export default class AnnotationLayer extends React.PureComponent {
annotation.color = annotation.color =
annotation.color === AUTOMATIC_COLOR ? null : annotation.color; annotation.color === AUTOMATIC_COLOR ? null : annotation.color;
this.props.addAnnotationLayer(annotation); this.props.addAnnotationLayer(annotation);
this.setState({ isNew: false, oldName: this.state.name }); this.setState(prevState => ({ isNew: false, oldName: prevState.name }));
} }
} }

View File

@ -47,19 +47,21 @@ export default class BoundsControl extends React.Component {
} }
onMinChange(event) { onMinChange(event) {
const min = event.target.value;
this.setState( this.setState(
{ prevState => ({
minMax: [event.target.value, this.state.minMax[1]], minMax: [min, prevState.minMax[1]],
}, }),
this.onChange, this.onChange,
); );
} }
onMaxChange(event) { onMaxChange(event) {
const max = event.target.value;
this.setState( this.setState(
{ prevState => ({
minMax: [this.state.minMax[0], event.target.value], minMax: [prevState.minMax[0], max],
}, }),
this.onChange, this.onChange,
); );
} }

View File

@ -259,14 +259,14 @@ class DateFilterControl extends React.Component {
const closeCalendar = const closeCalendar =
(key === 'since' && this.state.sinceViewMode === 'days') || (key === 'since' && this.state.sinceViewMode === 'days') ||
(key === 'until' && this.state.untilViewMode === 'days'); (key === 'until' && this.state.untilViewMode === 'days');
this.setState({ this.setState(prevState => ({
type: TYPES.CUSTOM_START_END, type: TYPES.CUSTOM_START_END,
[key]: typeof value === 'string' ? value : value.format(MOMENT_FORMAT), [key]: typeof value === 'string' ? value : value.format(MOMENT_FORMAT),
showSinceCalendar: this.state.showSinceCalendar && !closeCalendar, showSinceCalendar: prevState.showSinceCalendar && !closeCalendar,
showUntilCalendar: this.state.showUntilCalendar && !closeCalendar, showUntilCalendar: prevState.showUntilCalendar && !closeCalendar,
sinceViewMode: closeCalendar ? 'days' : this.state.sinceViewMode, sinceViewMode: closeCalendar ? 'days' : prevState.sinceViewMode,
untilViewMode: closeCalendar ? 'days' : this.state.untilViewMode, untilViewMode: closeCalendar ? 'days' : prevState.untilViewMode,
}); }));
} }
setTypeCustomRange() { setTypeCustomRange() {

View File

@ -90,10 +90,9 @@ export default class FixedOrMetricControl extends React.Component {
} }
toggle() { toggle() {
const expanded = !this.state.expanded; this.setState(prevState => ({
this.setState({ expanded: !prevState.expanded,
expanded, }));
});
} }
render() { render() {

View File

@ -114,7 +114,7 @@ export default class SpatialControl extends React.Component {
toggleCheckbox() { toggleCheckbox() {
this.setState( this.setState(
{ reverseCheckbox: !this.state.reverseCheckbox }, prevState => ({ reverseCheckbox: !prevState.reverseCheckbox }),
this.onChange, this.onChange,
); );
} }

View File

@ -120,7 +120,7 @@ export default class VizTypeControl extends React.PureComponent {
} }
toggleModal() { toggleModal() {
this.setState({ showModal: !this.state.showModal }); this.setState(prevState => ({ showModal: !prevState.showModal }));
} }
changeSearch(event) { changeSearch(event) {

View File

@ -176,16 +176,21 @@ class FilterBox extends React.Component {
vals = options; vals = options;
} }
} }
const selectedValues = {
...this.state.selectedValues,
[fltr]: vals,
};
this.setState({ selectedValues, hasChanged: true }, () => { this.setState(
if (this.props.instantFiltering) { prevState => ({
this.props.onChange({ [fltr]: vals }, false); selectedValues: {
} ...prevState.selectedValues,
}); [fltr]: vals,
},
hasChanged: true,
}),
() => {
if (this.props.instantFiltering) {
this.props.onChange({ [fltr]: vals }, false);
}
},
);
} }
/** /**