Forked from fredbegin11/mapStateToProps-vs-Selector.js
Created
February 11, 2020 23:25
-
-
Save reddyonrails/d815986502801b3d3af554a358e3b858 to your computer and use it in GitHub Desktop.
React selector use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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