react中怎么获取数据


本文摘自PHP中文网,作者coldplay.xixi,侵删。

react中获取数据的方法:1、使用生命周期方法请求数据;2、使用Hooks获取数据;3、使用suspense获取数据。

本教程操作环境:windows10系统、react16,本文适用于所有品牌的电脑。

react中获取数据的方法:

1.使用生命周期方法请求数据

应用程序Employees.org做两件事:

1.一进入程序就获取20名员工。

2.可以通过过滤条件来筛选员工。

在实现这两个需求之前,先来回顾一下react 类组件的2个生命周期方法:

  • componentDidMount():组件挂载后执行

  • componentDidUpdate(prevProps):当 props 或 state 改变时执行

组件 <EmployeesPage>使用上面两个生命周期方法实现获取逻辑:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import EmployeesList from "./EmployeesList";

import { fetchEmployees } from "./fake-fetch";

class EmployeesPage extends Component {

  constructor(props) {

    super(props);

    this.state = { employees: [], isFetching: true };

  }

  componentDidMount() {

    this.fetch();

  }

  componentDidUpdate(prevProps) {

    if (prevProps.query !== this.props.query) {

      this.fetch();

    }

  }

  async fetch() {

    this.setState({ isFetching: true });

    const employees = await fetchEmployees(this.props.query);

    this.setState({ employees, isFetching: false });

  }

  render() {

    const { isFetching, employees } = this.state;

    if (isFetching) {

      return <div>获取员工数据中...</div>;

    }

    return <EmployeesList employees={employees} />;

  }

}

打开codesandbox可以查看<EmployeesPage>获取过程。

<EmployeesPage>有一个获取数据的异步方法fetch()。在获取请求完成后,使用 setState 方法来更新employees。

this.fetch()componentDidMount()生命周期方法中执行:它在组件初始渲染时获取员工数据。

当咱们关键字进行过滤时,将更新 props.query 。每当 props.query 更新,componentDidUpdate()就会重新执行this.fetch()。

虽然生命周期方法相对容易掌握,但是基于类的方法存在样板代码使重用性变得困难。

优点

这种方法很容易理解:componentDidMount()在第一次渲染时获取数据,而componentDidUpdate()在props更新时重新获取数据。

缺点

样板代码

基于类的组件需要继承React.Component,在构造函数中执行 super(props) 等等。

this:使用 this 关键字很麻烦。

代码重复

componentDidMount()componentDidUpdate()中的代码大部分是重复的。

很难重用

员工获取逻辑很难在另一个组件中重用。

2.使用 Hooks 获取数据

Hooks 是基于类获取数据方式更好的选择。作为简单的函数,Hooks 不像类组件那样还要继承,并且也更容易重用。

简单回忆一下useEffect(callback[, deps]) Hook。这个hook在挂载后执行callback ,并且当依赖项deps发生变化时重新渲染。

如下示例所示,在<EmployeesPage>中使用useEffect()获取员工数据:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import EmployeesList from "./EmployeesList";

import { fetchEmployees } from "./fake-fetch";

function EmployeesPage({ query }) {

  const [isFetching, setFetching] = useState(false);

  const [employees, setEmployees] = useState([]);

  useEffect(function fetch() {

    (async function() {

      setFetching(true);

      setEmployees(await fetchEmployees(query));

      setFetching(false);

    })();

  }, [query]);

   

  if (isFetching) {

    return <div>Fetching employees....</div>;

  }

  return <EmployeesList employees={employees} />;

}

打开codesandbox可以查看useEffect()如何获取数据。

阅读剩余部分

相关阅读 >>

React中什么叫子组件

React中material-ui是什么

开发React用什么工具?

React有哪几种传递参数的方式

ReactReact native的区别是什么

使用React写一个网站的心得体会

React中怎么改变state的值

React hooks是什么?

React可以做什么?

webstorm写React出现报错怎么办

更多相关阅读请进入《React》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...