File tree Expand file tree Collapse file tree 1 file changed +35
-14
lines changed
docs/markdown/02-nodes-elements-components Expand file tree Collapse file tree 1 file changed +35
-14
lines changed Original file line number Diff line number Diff line change @@ -8,20 +8,20 @@ All React components **must** act like **pure functions** with respect to their
8
8
9
9
# Purity
10
10
11
- ## Pure function
11
+ ## Impure function
12
+
13
+ Do you know why this function is not pure ?
12
14
13
15
``` typescript
14
- function sum(a , b ) {
15
- return a + b ;
16
+ let count = 0 ;
17
+
18
+ function incrementCount(obj ) {
19
+ count ++ ;
20
+ obj .value = count ;
21
+ return count ;
16
22
}
17
23
```
18
24
19
- <!-- .element: class="big-code" -->
20
-
21
- - always returns the same result for a given set of parameters
22
- - does not modify its parameters
23
- - does not have semantical side effects
24
-
25
25
##==##
26
26
27
27
<!-- .slide: class="with-code" -->
@@ -30,15 +30,36 @@ function sum(a, b) {
30
30
31
31
## Impure function
32
32
33
+ Do you know why this function is not pure ?
34
+
33
35
``` typescript
34
- const fee = 0.5 ;
36
+ let count = 0 ;
35
37
36
- function withdraw(account , x ) {
37
- account .total -= x + fee ;
38
+ function incrementCount(obj ) {
39
+ count ++ ;
40
+ obj .value = count ;
41
+ return count ;
42
+ }
43
+ ```
44
+
45
+ - depends on external variable context
46
+ - creates side effect (updates external variable and argument)
47
+
48
+ ##==##
49
+
50
+ <!-- .slide: class="with-code" -->
51
+
52
+ # Purity
53
+
54
+ ## Pure function
55
+
56
+ ``` typescript
57
+ function sum(a , b ) {
58
+ return a + b ;
38
59
}
39
60
```
40
61
41
62
<!-- .element: class="big-code" -->
42
63
43
- - modifies its parameters
44
- - and/or depends on external context
64
+ - always returns the same result for a given set of parameters
65
+ - does not create side effects
You can’t perform that action at this time.
0 commit comments