Created
June 7, 2016 15:32
-
-
Save slightlytyler/e5da3412862f9e40787b7bb4f701457b to your computer and use it in GitHub Desktop.
Example of component + container
This file contains hidden or 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
import React, { Component, PropTypes } from 'react'; | |
import { ALL, ACTIVE, COMPLETED } from 'src/constants'; | |
class Filters extends Component { | |
static propTypes = { | |
currentFilter: PropTypes.oneOf([ | |
ALL, | |
ACTIVE, | |
COMPLETED, | |
]).isRequired, | |
setFilter: PropTypes.func.isRequired, | |
}; | |
renderItem = filter => { | |
const handleClick = () => this.props.setFilter(filter); | |
const classes = this.props.currentFilter === filter | |
? 'selected' | |
: ''; | |
return ( | |
<li onClick={handleClick}> | |
<a className={classes}>{filter}</a> | |
</li> | |
); | |
}; | |
render() { | |
return ( | |
<ul className="filters"> | |
{this.renderItem(ALL)} | |
{this.renderItem(ACTIVE)} | |
{this.renderItem(COMPLETED)} | |
</ul> | |
); | |
} | |
} | |
import { connect } from 'react-redux'; | |
import { bindActionCreators } from 'redux'; | |
import { setFilter } from 'src/actions'; | |
export default connect( | |
state => ({ currentFilter: state.filter }), | |
dispatch => bindActionCreators({ setFilter }, dispatch) | |
)(Filters); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment