Skip to content

Instantly share code, notes, and snippets.

@samsch
Last active March 17, 2016 13:43
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 samsch/a834b8ba0c142c0ba5c8 to your computer and use it in GitHub Desktop.
Save samsch/a834b8ba0c142c0ba5c8 to your computer and use it in GitHub Desktop.
Basic selectors idea.

The state shape would be something like:

{
    products: [
        {
            price: 7500,
            name: "Some product",
            //other details
        },
        {
            //More products
        },
        //...
    ],
    //other state
}
let React = require('react');
let { connect } = require('react-redux');
let totalPriceSelector = require('./totalPriceSelector');
let TotalPriceContainer = React.createClass({
render: function() {
return (
<div>
Total price: {this.props.totalPrice}
</div>
);
}
});
let selector = function(state) {
return {
//state, //You could include the raw state if you want, as well.
totalPrice: totalPriceSelector(state),
}
}
module.exports = connect(selector)(TotalPriceContainer);
let selector = function(state) {
let total = state.products.reduce((total, aProduct) => {
return total + aProduct.price;
}, 0);
}
module.exports = selector;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment