Skip to content

Commit 847492c

Browse files
authored
docs: Extended / updated documenation (#824)
* Initial improvements, modernized react, add sandboxes * touch
1 parent 7f41483 commit 847492c

File tree

8 files changed

+315
-170
lines changed

8 files changed

+315
-170
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"javascript.validate.enable": false,
33
"typescript.tsdk": "node_modules/typescript/lib",
4-
"jest.enableInlineErrorMessages": true
4+
"jest.enableInlineErrorMessages": true,
5+
"cSpell.enabled": true
56
}

website/docs/curried-produce.mdx

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ title: Curried producers
99
data-ea-type="image"
1010
className="horizontal bordered"
1111
></div>
12-
</center> <details>
12+
</center>
13+
<details>
1314
<summary className="egghead-summary">
1415
egghead.io lesson 6: Simplify code by using curried _reduce_
1516
</summary>
@@ -30,56 +31,50 @@ title: Curried producers
3031
</a>
3132
</details>
3233

33-
Passing a function as the first argument to `produce` is intended to be used for currying. This means that you get a pre-bound producer that only needs a state to produce the value from. The producer function gets passed in the draft and any further arguments that were passed to the curried function.
34-
35-
For example:
34+
Passing a function as the first argument to `produce` creates a function that doesn't apply `produce` yet to a specific state, but rather creates a function that will apply `produce` to any state that is passed to it in the future. This generally is called _currying_. Take for example the following example:
3635

3736
```javascript
38-
// mapper will be of signature (state, index) => state
39-
const mapper = produce((draft, index) => {
40-
draft.index = index
41-
})
37+
import produce from "immer"
38+
39+
function toggleTodo(state, id) {
40+
return produce(state, draft => {
41+
const todo = draft.find(todo => todo.id === id)
42+
todo.done = !todo.done
43+
})
44+
}
45+
46+
const baseState = [
47+
{
48+
id: "JavaScript",
49+
title: "Learn TypeScript",
50+
done: true
51+
},
52+
{
53+
id: "Immer",
54+
title: "Try Immer",
55+
done: false
56+
}
57+
]
4258

43-
// example usage
44-
console.dir([{}, {}, {}].map(mapper))
45-
//[{index: 0}, {index: 1}, {index: 2}])
59+
const nextState = toggleTodo(baseState, "Immer")
4660
```
4761

48-
This mechanism can also nicely be leveraged to further simplify our example reducer:
62+
The above pattern of `toggleTodo` is quite typical; pass an existing state to `produce`, modify the `draft`, and then return the result. Since `state` isn't used for anything else than passing it on to `produce`, the above example can be simplified by using the _curried_ form of `produce`, where you pass `produce` only the recipe function, and `produce` will return a new function that will apply recipe to the base state. This allows us to shorten the above `toggleTodo` definition.
4963

5064
```javascript
5165
import produce from "immer"
5266

53-
const byId = produce((draft, action) => {
54-
switch (action.type) {
55-
case RECEIVE_PRODUCTS:
56-
action.products.forEach(product => {
57-
draft[product.id] = product
58-
})
59-
return
60-
}
67+
// curried producer:
68+
const toggleTodo = produce((draft, id) => {
69+
const todo = draft.find(todo => todo.id === id)
70+
todo.done = !todo.done
6171
})
62-
```
63-
64-
Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it).
65-
66-
If you want to initialize an uninitialized state using this construction, you can do so by passing the initial state as second argument to `produce`:
6772

68-
```javascript
69-
import produce from "immer"
73+
const baseState = [
74+
/* as is */
75+
]
7076

71-
const byId = produce(
72-
(draft, action) => {
73-
switch (action.type) {
74-
case RECEIVE_PRODUCTS:
75-
action.products.forEach(product => {
76-
draft[product.id] = product
77-
})
78-
return
79-
}
80-
},
81-
{
82-
1: {id: 1, name: "product-1"}
83-
}
84-
)
77+
const nextState = toggleTodo(baseState, "Immer")
8578
```
79+
80+
Note that the `id` param has now become part of the recipe function! This pattern of having curried producers combines really neatly with for example the `useState` hook from React, as we will see on the next page.

website/docs/example-reducer.mdx

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)