Skip to content

Instantly share code, notes, and snippets.

@fredbegin11
Last active February 11, 2020 23:25
Show Gist options
  • Save fredbegin11/a0cf1a5c83c1667e0870bbc8e51b6cfb to your computer and use it in GitHub Desktop.
Save fredbegin11/a0cf1a5c83c1667e0870bbc8e51b6cfb 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