Last active
February 11, 2020 23:25
-
-
Save fredbegin11/a0cf1a5c83c1667e0870bbc8e51b6cfb 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