-
-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
6,943 additions
and
3,627 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,5 @@ typings/ | |
.idea | ||
/dist/ | ||
website/build | ||
website/.docusaurus | ||
.rts2* |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
id: example-setstate | ||
title: React setState example | ||
--- | ||
|
||
<center> | ||
<div | ||
data-ea-publisher="immerjs" | ||
data-ea-type="image" | ||
className="horizontal bordered" | ||
></div> | ||
</center> <details> | ||
<summary className="egghead-summary"> | ||
egghead.io lesson 8: Using Immer with _useState_. Or: _useImmer_ | ||
</summary> | ||
<br /> | ||
<div> | ||
<iframe | ||
width="760" | ||
height="427" | ||
scrolling="no" | ||
src="https://egghead.io/lessons/react-immutable-update-state-inside-react-components-with-useimmer/embed" | ||
></iframe> | ||
</div> | ||
<a | ||
className="egghead-link" | ||
href="https://egghead.io/lessons/react-immutable-update-state-inside-react-components-with-useimmer" | ||
> | ||
Hosted on egghead.io | ||
</a> | ||
</details> | ||
|
||
Deep updates in the state of React components can be greatly simplified as well by using immer. Take for example the following onClick handlers (Try in [codesandbox](https://codesandbox.io/s/m4yp57632j)): | ||
|
||
```javascript | ||
/** | ||
* Classic React.setState with a deep merge | ||
*/ | ||
onBirthDayClick1 = () => { | ||
this.setState(prevState => ({ | ||
user: { | ||
...prevState.user, | ||
age: prevState.user.age + 1 | ||
} | ||
})) | ||
} | ||
|
||
/** | ||
* ...But, since setState accepts functions, | ||
* we can just create a curried producer and further simplify! | ||
*/ | ||
onBirthDayClick2 = () => { | ||
this.setState( | ||
produce(draft => { | ||
draft.user.age += 1 | ||
}) | ||
) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.