Skip to content
This repository was archived by the owner on Sep 14, 2025. It is now read-only.

Commit 99b5d12

Browse files
author
vinogradov
committed
Add reselect and examples
1 parent 8587001 commit 99b5d12

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Name | Library Type | Original Description | Example Config | Notes
2828
[react](https://facebook.github.io/react/) | View layer | A javascript library for building user interfaces
2929
[react-router](https://github.com/ReactTraining/react-router) | Routing | Declarative routing for React
3030
[redux](https://github.com/reactjs/redux/) | Data management | Predictable state container for JavaScript apps
31+
[reselect](https://github.com/reduxjs/reselect) | Data management | Selector library for Redux
3132
[react-redux](https://github.com/reactjs/react-redux) | Data management | Official React bindings for Redux
3233
[redux-actions](https://github.com/acdlite/redux-actions) | Data management | Flux Standard Action utilities for Redux
3334
[redux-thunk](https://github.com/gaearon/redux-thunk) | Data management | Thunk middleware for Redux

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"redux-logger": "3.0.6",
5151
"redux-saga": "0.15.4",
5252
"redux-thunk": "2.2.0",
53+
"reselect": "3.0.1",
5354
"sass-loader": "6.0.6",
5455
"script-ext-html-webpack-plugin": "1.8.5",
5556
"style-loader": "0.18.2",

src/entry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import './examples/react/index';
88
// import './examples/redux/separate-files';
99
// import './examples/redux/separate-files-redux-actions';
1010
// import './examples/router';
11+
// import './examples/reselect/reselect-example-1';
1112
// import './examples/styles/cssmodules-className';
1213
// import './examples/styles/cssmodules-className-sass';
1314
// import './examples/styles/cssmodules-styleName';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createSelector } from 'reselect';
2+
3+
const shopItemsSelector = state => state.shop.items;
4+
const taxPercentSelector = state => state.shop.taxPercent;
5+
6+
const subtotalSelector = createSelector(
7+
shopItemsSelector,
8+
items => items.reduce((acc, item) => acc + item.value, 0)
9+
);
10+
11+
const taxSelector = createSelector(
12+
subtotalSelector,
13+
taxPercentSelector,
14+
(subtotal, taxPercent) => subtotal * (taxPercent / 100)
15+
);
16+
17+
export const totalSelector = createSelector(
18+
subtotalSelector,
19+
taxSelector,
20+
(subtotal, tax) => ({ total: subtotal + tax })
21+
);
22+
23+
const exampleState = {
24+
shop: {
25+
taxPercent: 8,
26+
items: [
27+
{ name: 'apple', value: 1.20 },
28+
{ name: 'orange', value: 0.95 },
29+
]
30+
}
31+
};
32+
33+
/* eslint-disable no-console */
34+
console.log(subtotalSelector(exampleState)); // 2.15
35+
console.log(taxSelector(exampleState)); // 0.172
36+
console.log(totalSelector(exampleState)); // { total: 2.322 }
37+
/* eslint-enable no-console */
38+

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5239,6 +5239,10 @@ requires-port@1.0.x, requires-port@1.x.x:
52395239
version "1.0.0"
52405240
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
52415241

5242+
reselect@3.0.1:
5243+
version "3.0.1"
5244+
resolved "https://registry.yarnpkg.com/reselect/-/reselect-3.0.1.tgz#efdaa98ea7451324d092b2b2163a6a1d7a9a2147"
5245+
52425246
resolve-from@^1.0.0:
52435247
version "1.0.1"
52445248
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226"

0 commit comments

Comments
 (0)