Skip to content

Commit

Permalink
Fix setState calls using updater functions (#3247)
Browse files Browse the repository at this point in the history
React does not guarantee that the state changes are applied immediately. It is best practice to use an updater function if the new state relies on the current state.
  • Loading branch information
Christian Ivicevic authored and giladgray committed Dec 16, 2018
1 parent ab3078a commit 8e1e03d
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export class PanelStackExample extends React.PureComponent<IExampleProps, IPanel
}

private addToPanelStack = (newPanel: IPanel) => {
this.setState({ currentPanelStack: [newPanel, ...this.state.currentPanelStack] });
this.setState(state => ({ currentPanelStack: [newPanel, ...state.currentPanelStack] }));
};

private removeFromPanelStack = (_lastPanel: IPanel) => {
// In this example, the last panel is always the one closed.
// Using `this.props.closePanel()` is one way to violate this.
this.setState({ currentPanelStack: this.state.currentPanelStack.slice(1) });
this.setState(state => ({ currentPanelStack: state.currentPanelStack.slice(1) }));
};
}

Expand Down

1 comment on commit 8e1e03d

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix setState calls using updater functions (#3247)

Previews: documentation | landing | table

Please sign in to comment.