Skip to content

Commit 7e19257

Browse files
s0afc163
authored andcommitted
[chore] fix inconsistent component state updates (#231)
React component state updates using setState may asynchronously update this.props and this.state, thus it is not safe to use either of the two when calculating the new state passed to setState, and instead the callback-based variant should be used instead.
1 parent 372b19b commit 7e19257

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

examples/selectedKeys.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class Test extends React.Component {
3434
onCheck = (e) => {
3535
const value = e.target.value;
3636
if (e.target.checked) {
37-
this.setState({
38-
selectedKeys: this.state.selectedKeys.concat(value),
39-
});
37+
this.setState(state => ({
38+
selectedKeys: state.selectedKeys.concat(value),
39+
}));
4040
} else {
4141
const selectedKeys = this.state.selectedKeys.concat();
4242
const index = selectedKeys.indexOf(value);
@@ -52,9 +52,9 @@ class Test extends React.Component {
5252
onOpenCheck = (e) => {
5353
const value = e.target.value;
5454
if (e.target.checked) {
55-
this.setState({
56-
openKeys: this.state.openKeys.concat(value),
57-
});
55+
this.setState(state => ({
56+
openKeys: state.openKeys.concat(value),
57+
}));
5858
} else {
5959
const openKeys = this.state.openKeys.concat();
6060
const index = openKeys.indexOf(value);

0 commit comments

Comments
 (0)