Cannot call callback functions on setState in componentDidMount ?? - React

TheWebs

Consider the following simple code:

  componentDidMount() {
    this._fetchData();
  }

  _fetchData() {
    let url = UrlFormatter() + '/api/v1/blogs/';

    $.get(url, (result) => {
      if (result.status === 401) {
        this.setState({
          error: 'Your session has expired. We cannot load data.',
        });
      } else {
        console.log('obvs here');
        this.setState({
          error:             null,
          data:              result,
        }, () => {
          console.log('dasddsa');
          this._setUpPostCollapseStatus();
        });
      }
    }).fail((response) => {
      this.setState({
        error: 'Could not fetch blogs, something went wrong.'
      });
    });
  }

If we investigate the console we see:

obvs here

But we never see: dasddsa, now either this is a bug, or you cant call a callback function on setState in componentDidMount - Or I fail at ES6.

Ideas?

lux

Hm, I wasn't able to replicate this; not sure if this'll be helpful, but here's an example of resolving a promise in componentDidMount and using the setState callback:

http://codepen.io/mikechabot/pen/dXWQAr?editors=0011

promise

const promise = new Promise(resolve => {
  setTimeout(() => {
    resolve('Fetched data!')
  }, 2000)
})

component

  componentDidMount() {
    console.log('Mounting...');
    promise
      .then((data) => {
        this.setState({ data }, () => {
          console.log('Data loaded')
        })
      })
      .catch(error => {
        console.log('Error', error);
      })
  }

console

> "Mounting..."
> "Data loaded"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use `setState` callback on react hooks

From Java

React: Cant call setState on a component that is not yet mounted

From Dev

React.js :You called `setState` with a callback that isn't callable

From Dev

React Native setState(…) warning with both componentWillMount and componentDidMount

From Dev

Using the Request callback in NPM to call additional functions

From Dev

setstate cannot update during an state transition - react

From Dev

Cannot call callback functions on setState in componentDidMount ?? - React

From Dev

Why cannot we call prototype functions?

From Dev

React SetState doesn't call render

From Dev

React - setState inside componentDidMount

From Dev

React: TypeError: Cannot read property 'setState' of undefined

From Dev

Replace of setState callback in react hook with useEffect hooks for complicated scenario not working

From Dev

React: Cannot call functions from another file

From Dev

Is there a way for an AJAX call to return both HTML content and callback functions?

From Dev

Call other functions from a callback function

From Dev

React componentDidMount not firing for jQuery AJAX call

From Dev

Cannot read property 'setState' of null react

From Dev

Cannot read property 'setState' of undefined in AJAX call

From Dev

Warning: setState(...) on ComponentDidMount when navigating using react-router

From Dev

React : setState in componentDidMount not causing infinite loop

From Dev

React - Cannot get property setState of null

From Dev

JavaScript functions cannot be call one from another

From Dev

react child component fails to render when calling parent callback with setstate

From Dev

Callback for setState in React inside map()

From Dev

How to write a callback function for asynchronous calls from helper method to componentDidMount using React JS

From Dev

Can't Call setState on an unmounted component even after componentDidMount

From Dev

Double rendering in React with asynchronous call in componentDidMount causing error

From Dev

When use ComponentDidMount() I found this error : Can't call setState

From Dev

socket.io anonymous callback: can't call class functions

Related Related

  1. 1

    How to use `setState` callback on react hooks

  2. 2

    React: Cant call setState on a component that is not yet mounted

  3. 3

    React.js :You called `setState` with a callback that isn't callable

  4. 4

    React Native setState(…) warning with both componentWillMount and componentDidMount

  5. 5

    Using the Request callback in NPM to call additional functions

  6. 6

    setstate cannot update during an state transition - react

  7. 7

    Cannot call callback functions on setState in componentDidMount ?? - React

  8. 8

    Why cannot we call prototype functions?

  9. 9

    React SetState doesn't call render

  10. 10

    React - setState inside componentDidMount

  11. 11

    React: TypeError: Cannot read property 'setState' of undefined

  12. 12

    Replace of setState callback in react hook with useEffect hooks for complicated scenario not working

  13. 13

    React: Cannot call functions from another file

  14. 14

    Is there a way for an AJAX call to return both HTML content and callback functions?

  15. 15

    Call other functions from a callback function

  16. 16

    React componentDidMount not firing for jQuery AJAX call

  17. 17

    Cannot read property 'setState' of null react

  18. 18

    Cannot read property 'setState' of undefined in AJAX call

  19. 19

    Warning: setState(...) on ComponentDidMount when navigating using react-router

  20. 20

    React : setState in componentDidMount not causing infinite loop

  21. 21

    React - Cannot get property setState of null

  22. 22

    JavaScript functions cannot be call one from another

  23. 23

    react child component fails to render when calling parent callback with setstate

  24. 24

    Callback for setState in React inside map()

  25. 25

    How to write a callback function for asynchronous calls from helper method to componentDidMount using React JS

  26. 26

    Can't Call setState on an unmounted component even after componentDidMount

  27. 27

    Double rendering in React with asynchronous call in componentDidMount causing error

  28. 28

    When use ComponentDidMount() I found this error : Can't call setState

  29. 29

    socket.io anonymous callback: can't call class functions

HotTag

Archive