Skip to content

Instantly share code, notes, and snippets.

@slightlytyler
Created June 7, 2016 15:32
Show Gist options
  • Save slightlytyler/e5da3412862f9e40787b7bb4f701457b to your computer and use it in GitHub Desktop.
Save slightlytyler/e5da3412862f9e40787b7bb4f701457b to your computer and use it in GitHub Desktop.
Example of component + container
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