Skip to content

Commit 4082bc7

Browse files
authored
Update README.md
Using this.state in setState is discouraged due to potential timing issues. Might want to rethink the union type example, addition to null would give a type error and the fix looks unwieldy.
1 parent 803b470 commit 4082bc7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ class App extends React.Component<{
147147
);
148148
}
149149
increment = (amt: number) => { // like this
150-
this.setState({
151-
count: this.state.count + amt
152-
});
150+
this.setState(state => ({
151+
count: state.count + amt
152+
}));
153153
}
154154
}
155155
```
@@ -422,9 +422,9 @@ class App extends React.Component<{}, {
422422
);
423423
}
424424
increment = (amt: number) => {
425-
this.setState({
426-
count: this.state.count + amt
427-
});
425+
this.setState(state => ({
426+
count: (state.count || 0) + amt
427+
}));
428428
}
429429
}
430430
```

0 commit comments

Comments
 (0)