Skip to content

Commit e0278a6

Browse files
committed
redux compose
1 parent 29c3439 commit e0278a6

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

React/controlled-unContolled-Component.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class ControlledForm extends Component {
3232
}
3333
```
3434

35+
#### A user types into the input box → onChange is triggered invoking the updateUsername function. → the state of our component is set to a new value → React re-renders the virtual DOM → React Diffs the change → Real DOM is updated.
36+
3537
An uncontrolled component is where your form data is handled by the DOM, instead of inside your React component.
3638

3739
You use refs to accomplish this.

Redux/compose.md

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
Compose is used when you want to pass multiple store enhancers to the store. Store enhancers are higher order functions that add some extra functionality to the store. The only store enhancer which is supplied with Redux by default is applyMiddleware however many other are available.
2+
3+
Store Enhancers are Higher Order Functions
4+
5+
What are higher order functions? Paraphrased from the Haskell docs:
6+
7+
Higher order functions can take functions as parameters and return functions as return values. A function that does either of those is called a higher order function
8+
9+
From the Redux docs:
10+
11+
All compose does is let you write deeply nested function transformations without the rightward drift of the code. Don’t give it too much credit!
12+
13+
From the Redux docs if we don't use compose we would have
14+
15+
```js
16+
finalCreateStore = applyMiddleware(middleware)(
17+
require('redux-devtools').devTools()(
18+
require('redux-devtools').persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))()
19+
)
20+
)(createStore);
21+
```
22+
### Whereas if we use compose
23+
24+
```js
25+
finalCreateStore = compose(
26+
applyMiddleware(...middleware),
27+
require('redux-devtools').devTools(),
28+
require('redux-devtools').persistState(
29+
window.location.href.match(/[?&]debug_session=([^&]+)\b/)
30+
)
31+
)(createStore);
32+
```
33+
134

235
```js
336
const {
@@ -16,4 +49,22 @@ const composeAllThreeFromRightToLeft = compose(makeLouder, embolden, repeatThree
1649

1750
```
1851

19-
composeAllThreeFromRightToLeft('Hello!') will output "<B>HELLO!HELLO!HELLO!</B>"
52+
composeAllThreeFromRightToLeft('Hello!') will output "<B>HELLO!HELLO!HELLO!</B>"
53+
54+
55+
### Compose another example - Here is a very simple example of a function that composes two functions to return a new specialized function:
56+
57+
```js
58+
var greet = function(x) { return `Hello, ${ x }` };
59+
var emote = function(x) { return `${x} :)` };
60+
61+
var compose = function(f, g) {
62+
return function(x) {
63+
return f(g(x));
64+
}
65+
}
66+
67+
var happyGreeting = compose(greet, emote);
68+
console.log(happyGreeting("Mark")); // => "Mark"
69+
70+
```

Redux/redux-thunk-what-is-thunk-in-programming.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 1> What are Thunks?
22

3-
The precise definition of a “thunk” varies across contexts. Generally though, thunks are a functional programming technique used to delay computation. Instead of performing some work now, you produce a function body or unevaluated expression (the “thunk”) which can optionally be used to perform the work later. Compare:
3+
Thunks are a functional programming technique used to delay computation. Instead of performing some work now, you produce a function body or unevaluated expression (the “thunk”) which can optionally be used to perform the work later. Compare:
44

55
```js
66
// Eager version
@@ -42,7 +42,6 @@ https://medium.com/fullstack-academy/thunks-in-redux-the-basics-85e538a3fe60
4242

4343

4444

45-
4645
2> In computer programming, a thunk is a subroutine used to inject an additional calculation into another subroutine. Thunks are primarily used to delay a calculation until its result is needed, or to insert operations at the beginning or end of the other subroutine. They have a variety of other applications in compiler code generation and modular programming.
4746

4847
https://en.wikipedia.org/wiki/Thunk

0 commit comments

Comments
 (0)