使用 ajax 设置反应组件的初始状态

Set inital state of react component with ajax

我目前正在尝试使用一些 React 组件来增强我的项目。 我已经成功构建了该应用程序,它可以满足我的要求。

现在我需要删除包含一些数据的虚拟数组,并用我的数据库中的真实数据填充它。

通常我会用 ajax 来解析它。

我查看了 facebook 文档,发现了以下代码片段:

...    
componentDidMount: function() {
        $.get(this.props.source, function(result) {
          var lastGist = result[0];
          if (this.isMounted()) {
            this.setState({
              username: lastGist.owner.login,
              lastGistUrl: lastGist.html_url
            });
          }
        }.bind(this));
      },
...

现在我的问题是,应该如何以及何时在反应组件中调用此函数。或者它是一个保存的名称并被自动调用?

谢谢:)

is it a saved name and gets called automatically?

是。

https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount