Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddyonrails/d815986502801b3d3af554a358e3b858 to your computer and use it in GitHub Desktop.
Save reddyonrails/d815986502801b3d3af554a358e3b858 to your computer and use it in GitHub Desktop.
React selector use case
// Store Redux
state = {
cars: {
carsById: {
1: { id: 1, name "Toyota" },
2: { id: 2, name "Lexus" },
3: { id: 3, name "Honda" }
}
}
};
// selectors.js
const carsSelector = state => state.cars.carsById;
export const getCarsSelector = createSelector(carsSelector, carsById => Object.values(carsById));
// CarListPage.js
class CarListPage extends PureComponent {
...
render() {
return (
<div className="myClass">
{this.props.cars.map(car => <CarComponent car={car} />)}
</div>
);
};
...
}
const mapStateToProps = state => {
return {
cars: getCarsSelector(state)
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment