Skip to content

Instantly share code, notes, and snippets.

@na0x2c6
Created August 22, 2018 09:01
Show Gist options
  • Save na0x2c6/cba8db40ccbb3c8db18008ab25abd880 to your computer and use it in GitHub Desktop.
Save na0x2c6/cba8db40ccbb3c8db18008ab25abd880 to your computer and use it in GitHub Desktop.
HOC Sample 1: No HOC
const Card = ({ card }) => (
<div className="card">
<div className="card-content" style={{ color: card.color }}>
{card.name}
</div>
</div>
)
class List extends React.Component {
constructor(props) {
super(props)
this.state = {
cards: DataSource.getCards(props.list.id),
}
this.handleChange = this.handleChange.bind(this)
}
componentDidMount() {
DataSource.addChangeListener(this.handleChange);
}
componentWillUnmount() {
DataSource.removeChangeListener(this.handleChange);
}
handleChange() {
this.setState({
cards: DataSource.getCards(this.props.list.id)
})
}
render() {
return (
<div className="column"
style={{ backgroundColor: this.props.list.color }}>
<header>{`${this.props.list.name} (id:${this.props.list.id})`}</header>
{this.state.cards.map((card, idx) =>
<Card card={card} key={idx} />
)}
</div>
)
}
}
class Board extends React.Component {
constructor(props) {
super(props)
this.state = {
lists: DataSource.getLists()
}
this.handleChange = this.handleChange.bind(this)
}
componentDidMount() {
DataSource.addChangeListener(this.handleChange);
}
componentWillUnmount() {
DataSource.removeChangeListener(this.handleChange);
}
handleChange() {
this.setState({
lists: DataSource.getLists()
})
}
render() {
return (
<div className="columns">
{this.state.lists.map(list =>
<List list={list} key={list.id} />
)}
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment