Reselect: Retaining memoization when passing props to *different* component instances.

Created on 1 Feb 2018  路  2Comments  路  Source: reduxjs/reselect

The docs state that in order to share a selector across multiple component instances while passing in props and retaining memoization, each instance of the component needs its own private copy of the selector. Does this apply to instances of different components using the same selector?

selectors.js

import {createSelector} from 'reselect';
getData = (state, props) => state.data[props.id];
// additional question: is reselect worth using for such a simple selector?
export const getDataForId = createSelector(getData, data => data);

foo.js

import {getDataForId} from './selectors';
const mapStateToProps = (state, props) => ({
  dataForId: getDataForId(state, props),
});
class Foo extends Component {...}
export default connect(mapStateToProps)(Foo);

bar.js

import {getDataForId} from './selectors';
const mapStateToProps = (state, props) => ({
  dataForId: getDataForId(state, props), // uses same selector as Foo
});
class Bar extends Component {...}
export default connect(mapStateToProps)(Bar);

index.js

...
<Foo />
<Bar />
...

Most helpful comment

All 2 comments

I wish I found that article 馃挴 sooner! 猬嗭笍

Was this page helpful?
0 / 5 - 0 ratings