Skip to content

Instantly share code, notes, and snippets.

@gnapse
Last active November 29, 2017 20:57
Show Gist options
  • Select an option

  • Save gnapse/ea53bcedf94a1a32b42bfb71e50817ff to your computer and use it in GitHub Desktop.

Select an option

Save gnapse/ea53bcedf94a1a32b42bfb71e50817ff to your computer and use it in GitHub Desktop.

Revisions

  1. gnapse revised this gist Nov 29, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions MyComponent.js
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,8 @@ export default class MyComponent extends Component {

    componentDidMount() {
    fetch('/api/data')
    .then(this.handleSuccess)
    .catch(this.handleFailure);
    .then(data => this.handleSuccess(data))
    .catch(error => this.handleFailure(error));
    }

    componentWillUnmount() {
  2. gnapse revised this gist Nov 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyComponent.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ export default class MyComponent extends Component {
    };

    componentDidMount() {
    fetch(/api/data)
    fetch('/api/data')
    .then(this.handleSuccess)
    .catch(this.handleFailure);
    }
  3. gnapse created this gist Nov 29, 2017.
    30 changes: 30 additions & 0 deletions MyComponent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    import React, { Component } from ‘react’;

    const noop = () => {};

    export default class MyComponent extends Component {
    state = { data: null, error: null };

    handleSuccess = data => {
    this.setState({ data });
    };

    handleFailure = error => {
    this.setState({ error });
    };

    componentDidMount() {
    fetch(/api/data’)
    .then(this.handleSuccess)
    .catch(this.handleFailure);
    }

    componentWillUnmount() {
    this.handleSuccess = noop;
    this.handleFailure = noop;
    }

    render() {
    // ...
    }
    }