Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: prevent usage of this.state inside setState calls #2302

Draft
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"no-console": 0,
"no-case-declarations": 0,
"quotes": ["error", "single"],
"eol-last": ["error", "always"]
"eol-last": ["error", "always"],
"react/no-access-state-in-setstate": 1
}
}
2 changes: 1 addition & 1 deletion src/components/BrowserCell/BrowserCell.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class BrowserCell extends Component {
classes.push(styles.required);
}

this.setState({ ...this.state, content, classes })
this.setState({ content, classes })
}

componentDidUpdate(prevProps) {
Expand Down
12 changes: 6 additions & 6 deletions src/components/Calendar/Calendar.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export default class Calendar extends React.Component {
}

handlePrev() {
this.setState({
currentMonth: prevMonth(this.state.currentMonth)
});
this.setState((prev) => ({
currentMonth: prevMonth(prev.currentMonth)
}));
}

handleNext() {
this.setState({
currentMonth: nextMonth(this.state.currentMonth)
});
this.setState((prev) => ({
currentMonth: nextMonth(prev.currentMonth)
}));
}

renderMonth() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/CascadingView/CascadingView.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class CascadingView extends React.Component {
<div
className={classes.join(' ')}
style={style}
onClick={() => this.setState({ expanded: !this.state.expanded })}>
onClick={() => this.setState((prev) => ({ expanded: !prev.expanded }))}>
<span className={styles.left}>{content}</span>
{expander}
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ChromeDatePicker/ChromeDatePicker.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default class ChromeDatePicker extends React.Component {
}

toggle() {
this.setState(() => {
if (this.state.open) {
this.setState((prev) => {
if (prev.open) {
return { open: false };
}
let pos = Position.inWindow(this.wrapRef.current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default class ColumnsConfiguration extends React.Component {
}

toggle() {
this.setState({
open: !this.state.open
})
this.setState((prev) => ({
open: !prev.open
}))
}

showAll() {
Expand Down
4 changes: 2 additions & 2 deletions src/components/DatePicker/DatePicker.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default class DatePicker extends React.Component {
}

toggle() {
this.setState(() => {
if (this.state.open) {
this.setState((prev) => {
if (prev.open) {
return { open: false };
}
return {
Expand Down
28 changes: 16 additions & 12 deletions src/components/DateRange/DateRange.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export default class DateRange extends React.Component {
}

toggle() {
this.setState(() => {
if (this.state.open) {
this.setState((prev) => {
if (prev.open) {
return { open: false };
}
let pos = Position.inWindow(this.wrapRef.current);
Expand All @@ -52,19 +52,23 @@ export default class DateRange extends React.Component {
}

setStart(start) {
let end = this.state.end;
if (start > end) {
end = daysFrom(start, 1);
}
this.setState({ start, end });
this.setState((prev) => {
let end = prev.end;
if (start > end) {
end = daysFrom(start, 1);
}
return { start, end };
});
}

setEnd(end) {
let start = this.state.start;
if (start > end) {
start = daysFrom(end, -1);
}
this.setState({ start, end });
this.setState((prev) => {
let start = prev.start;
if (start > end) {
start = daysFrom(end, -1);
}
return { start, end };
});
}

close() {
Expand Down
4 changes: 2 additions & 2 deletions src/components/DateTimeInput/DateTimeInput.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default class DateTimeInput extends React.Component {
}

toggle() {
this.setState(() => {
if (this.state.open) {
this.setState((prev) => {
if (prev.open) {
return { open: false };
}
let node = this.inputRef.current;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Dropdown/Dropdown.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default class Dropdown extends React.Component {
}

toggle() {
this.setState(() => {
if (this.state.open) {
this.setState((prev) => {
if (prev.open) {
return { open: false };
}
let pos = Position.inDocument(this.dropdownRef.current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export default class ExplorerActiveChartButton extends React.Component {
}

handleCheckbox() {
let nextActiveState = !this.state.active;
this.props.onToggle(nextActiveState);
this.setState({
active: nextActiveState
this.setState((prev) => ({
active: !prev.active
}), () => {
this.props.onToggle(this.state.active);
});
}

Expand Down Expand Up @@ -67,11 +67,11 @@ export default class ExplorerActiveChartButton extends React.Component {
position.x += this.wrapRef.current.clientWidth;
align = Directions.RIGHT;
}
this.setState({
open: !this.state.open,
this.setState((prev) => ({
open: !prev.open,
position,
align
});
}));
}} />
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ExplorerMenuButton/ExplorerMenuButton.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export default class ExplorerMenuButton extends React.Component {
}

toggle() {
this.setState(() => {
if (this.state.currentView) {
this.setState((prev) => {
if (prev.currentView) {
return { currentView: null };
}
let position = Position.inDocument(this.wrapRef.current);
Expand Down
Loading