简体   繁体   English

无法在componentDidMount中的setState上调用回调函数? -反应

[英]Cannot call callback functions on setState in componentDidMount ?? - React

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. 但是我们再也看不到: dasddsa ,现在这是一个错误,或者您不能在componentDidMount setState上调用回调函数-否则我在ES6上失败。

Ideas? 想法?

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: 不知道这是否会有所帮助,但是这里有一个在componentDidMount中解决一个promise并使用setState回调的示例:

http://codepen.io/mikechabot/pen/dXWQAr?editors=0011 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在reactjs中是否可以在componentDidMount中执行异步函数并在结果回调中执行setstate? - In reactjs is it ok to execute async functions in componentDidMount and do a setstate in the resulting callback? 在ComponentDidMount中反应警告,setState(...) - React Warning, setState(…) in ComponentDidMount setState 在 componentDidMount() 中不起作用 - React - setState not working in componentDidMount() - React componentDidMount()中的setState在地理位置回调中不起作用 - setState in componentDidMount() not working in geolocation callback 反应setstate返回未定义在componentDidMount() - React setstate return undefined on componentDidMount() 反应 componentDidMount setState 但返回未定义 - React componentDidMount setState but return undefined 无法理解 React setState 签名“setState(updater[, callback])” - Cannot understand React setState signature "setState(updater[, callback])" 使用 this.setState 回调 function 在 componentDidMount 中不起作用 - Callback function with this.setState not working in componentDidMount 在 componentDidMount() 中反应 Axios 调用 - React Axios call in componentDidMount() 无法使用componentDidMount从API提取数据:无法在已卸载的组件上调用setState(或forceUpdate) - Cannot fetch data from an API using componentDidMount: Can't call setState (or forceUpdate) on an unmounted component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM