Created
August 20, 2019 15:48
-
-
Save e-schultz/c5dbd7d5bed711e2783b93be5195c6a8 to your computer and use it in GitHub Desktop.
react hooks - lifecycle
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
| export default SearchComponent extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| results: [] | |
| } | |
| } | |
| componentDidMount() { | |
| this.query(this.props.id) | |
| } | |
| componentDidUpdate(prevProps) { | |
| if(this.prevProps.id !== this.props.id) { | |
| this.query(this.props.id); | |
| } | |
| } | |
| query(id) { | |
| this.setState({isLoading: true}) | |
| fetch(`/some/url/${id}`) | |
| .then(r=>r.json()) | |
| .then(r=>this.setState({ | |
| results: results | |
| }); | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In line 22,
results: r