Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): translate docs/faq-ajax.md into Chinese #157

Merged
merged 12 commits into from Apr 1, 2019
20 changes: 10 additions & 10 deletions content/docs/faq-ajax.md
Expand Up @@ -6,19 +6,19 @@ layout: docs
category: FAQ
---

### How can I make an AJAX call? {#how-can-i-make-an-ajax-call}
### 如何在 React 中发起 AJAX 请求?{#how-can-i-make-an-ajax-call}

You can use any AJAX library you like with React. Some popular ones are [Axios](https://github.com/axios/axios), [jQuery AJAX](https://api.jquery.com/jQuery.ajax/), and the browser built-in [window.fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
在 React 开发中,你能使用任何你喜欢的 AJAX 库,比如社区比较流行的 [Axios](https://github.com/axios/axios)[jQuery AJAX](https://api.jquery.com/jQuery.ajax/),或者是浏览器内置的 [window.fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)

### Where in the component lifecycle should I make an AJAX call? {#where-in-the-component-lifecycle-should-i-make-an-ajax-call}
### 应该在 React 组件的哪个生命周期函数中发起 AJAX 请求?{#where-in-the-component-lifecycle-should-i-make-an-ajax-call}

You should populate data with AJAX calls in the [`componentDidMount`](/docs/react-component.html#mounting) lifecycle method. This is so you can use `setState` to update your component when the data is retrieved.
我们推荐你在 [`componentDidMount`](/docs/react-component.html#mounting) 这个生命周期函数中发起 AJAX 请求。这样做你可以拿到 AJAX 请求返回的数据并通过 `setState` 来更新组件。

### Example: Using AJAX results to set local state {#example-using-ajax-results-to-set-local-state}
### 示例:使用 AJAX 请求结果去改变组件内部 state {#example-using-ajax-results-to-set-local-state}

The component below demonstrates how to make an AJAX call in `componentDidMount` to populate local component state.
下面这个组件演示了如何在 `componentDidMount` 中发起 AJAX 请求去更新组件的 state

The example API returns a JSON object like this:
示例 API 返回如下的 JSON 对象:

```
{
Expand Down Expand Up @@ -50,9 +50,9 @@ class MyComponent extends React.Component {
items: result.items
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
// 注意:需要在此处处理错误
// 而不是使用 catch() 去捕获错误
// 因为使用 catch 去捕获异常会掩盖掉组件本身可能产生的 bug
(error) => {
this.setState({
isLoaded: true,
Expand Down