|
@@ -0,0 +1,63 @@
|
|
|
+import { useState, useEffect } from "react";
|
|
|
+import './loading.css'
|
|
|
+function Word() {
|
|
|
+ return <div>正在加载中...</div>;
|
|
|
+}
|
|
|
+
|
|
|
+function Loading() {
|
|
|
+ let [list, setList] = useState([]);
|
|
|
+ let [load, setLoad] = useState(false);
|
|
|
+ useEffect(() => {
|
|
|
+ // 请求数据前展示loading样式
|
|
|
+ setLoad(true);
|
|
|
+ // http请求
|
|
|
+ fetch("/data.json")
|
|
|
+ .then((res) => res.json())
|
|
|
+ .then((arr) => {
|
|
|
+ setTimeout(() => {
|
|
|
+ setList(arr);
|
|
|
+ }, 2000);
|
|
|
+ })
|
|
|
+ .finally(() => {
|
|
|
+ setLoad(false);
|
|
|
+ });
|
|
|
+ }, []);
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ <h3>这是一个案例</h3>
|
|
|
+ <table className="vase">
|
|
|
+ <thead>
|
|
|
+ <tr>
|
|
|
+ <th>##</th>
|
|
|
+ <th>名字</th>
|
|
|
+ <th>年龄</th>
|
|
|
+ <th>住址</th>
|
|
|
+ </tr>
|
|
|
+ </thead>
|
|
|
+ <tbody>
|
|
|
+ {/* 数据未加载时候 展示loading样式 */}
|
|
|
+ {/* 数据加载时 展示表格数据 */}
|
|
|
+ {/* 三元判断 */}
|
|
|
+ {list.length === 0 ? (
|
|
|
+ <tr>
|
|
|
+ <td>
|
|
|
+ <Word />
|
|
|
+ </td>
|
|
|
+ </tr>
|
|
|
+ ) : (
|
|
|
+ list.map((item, index) => (
|
|
|
+ <tr key={index}>
|
|
|
+ <td>{index + 1}</td>
|
|
|
+ <td>{item.name}</td>
|
|
|
+ <td>{item.age}</td>
|
|
|
+ <td>{item.address}</td>
|
|
|
+ </tr>
|
|
|
+ ))
|
|
|
+ )}
|
|
|
+ </tbody>
|
|
|
+ </table>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+export default Loading;
|