Last active
October 22, 2018 20:10
-
-
Save James-E-Adams/2aa039740c3d306436f2e7e758a576e5 to your computer and use it in GitHub Desktop.
Functional React Article Snippet #1
This file contains 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
//------------------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