File size: 1,859 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# `reselect`
reselect memoizes ("caches") previous state trees and calculations based on said
tree. This means repeated changes and calculations are fast and efficient,
providing us with a performance boost over standard `mapStateToProps`
implementations.
The [official documentation](https://github.com/reactjs/reselect)
offers a good starting point!
## Usage
There are two different kinds of selectors, simple and complex ones.
### Simple selectors
Simple selectors are just that: they take the application state and select a
part of it.
```javascript
const mySelector = state => state.get('someState');
export { mySelector };
```
### Complex selectors
If we need to, we can combine simple selectors to build more complex ones which
get nested state parts with reselect's `createSelector` function. We import other
selectors and pass them to the `createSelector` call:
```javascript
import { createSelector } from 'reselect';
import mySelector from 'mySelector';
const myComplexSelector = createSelector(mySelector, myState =>
myState.get('someNestedState'),
);
export { myComplexSelector };
```
These selectors can then either be used directly in our containers as
`mapStateToProps` functions or be nested with `createSelector` once again:
```javascript
export default connect(
createSelector(myComplexSelector, myNestedState => ({ data: myNestedState })),
)(SomeComponent);
```
### Adding a new selector
If you have a `selectors.js` file next to the reducer which's part of the state
you want to select, add your selector to said file. If you don't have one yet,
add a new one into your container folder and fill it with this boilerplate code:
```JS
import { createSelector } from 'reselect';
const selectMyState = () => createSelector(
);
export {
selectMyState,
};
```
---
_Don't like this feature? [Click here](remove.md)_
|