Skip to content

Commit

Permalink
Merge pull request #2160 from pReya/patch-1
Browse files Browse the repository at this point in the history
Use syntax highlighting for examples in "no-access-state-in-setstate"
  • Loading branch information
ljharb committed Feb 14, 2019
2 parents 4270205 + 22d9a5e commit bc976b8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/rules/no-access-state-in-setstate.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Such usage of `this.state` might result in errors when two state calls are
called in batch and thus referencing old state and not the current
state. An example can be an increment function:

```
```javascript
function increment() {
this.setState({value: this.state.value + 1});
}
Expand All @@ -14,15 +14,15 @@ function increment() {
If these two `setState` operations is grouped together in a batch it will
look be something like the following, given that value is 1:

```
```javascript
setState({value: 1 + 1})
setState({value: 1 + 1})
```

This can be avoided with using callbacks which takes the previous state
as first argument:

```
```javascript
function increment() {
this.setState(prevState => ({value: prevState.value + 1}));
}
Expand All @@ -33,7 +33,7 @@ even when things happen in batches. And the example above will be
something like:


```
```javascript
setState({value: 1 + 1})
setState({value: 2 + 1})
```

0 comments on commit bc976b8

Please sign in to comment.