Skip to content

Instantly share code, notes, and snippets.

@James-E-Adams
Last active October 22, 2018 20:10
Show Gist options
  • Save James-E-Adams/2aa039740c3d306436f2e7e758a576e5 to your computer and use it in GitHub Desktop.
Save James-E-Adams/2aa039740c3d306436f2e7e758a576e5 to your computer and use it in GitHub Desktop.
Functional React Article Snippet #1
//------------------Written as a class---------------------//
class Farm extends React.Component {
constructor(props) {
super(props)
this.state = {
tools: ["hammer", "scythe", "sickle"]
}
}
componentDidMount() {
fetch("/tools")
.then(({ body: { tools } }) => this.setState(tools))
.catch(err =>
console.log("Oh no, your tools got lost. Here's why: ", err)
)
}
shouldComponentUpdate(nextProps, nextState) {
return nextState.tools !== this.state.tools
}
render() {
const { tools } = this.state
const { someProp, anotherProp } = this.props
if (!(tools && tools.length)) {
return null
}
return (
<div>
Here are all the tools in the farm:
<ul>
{tools.map((tool, index) => (
<li key={index}> {tool} </li>
))}
</ul>
{someProp} - {anotherProp}
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment