Created
August 22, 2018 09:01
-
-
Save na0x2c6/cba8db40ccbb3c8db18008ab25abd880 to your computer and use it in GitHub Desktop.
HOC Sample 1: No HOC
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
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