import { useState, useEffect } from 'react';
import { getStuListApi } from "../api/stuApi";
import Alert from "./Alert";
import { useLocation, NavLink } from "react-router-dom"
function Home(props) {
const [stuList, setStuList] = useState([]); // 存储所有的数据
const [searchItem, setSearchItem] = useState(""); // 存储用户输入的搜索信息
const [alert, setAlert] = useState(null);
const [searchList, setSearchList] = useState([]); // 存储搜索后的数据
const location = useLocation();
// 注意,这里需要添加依赖性为空数组,代表只执行一次
useEffect(() => {
getStuListApi().then(({ data }) => {
setStuList(data);
});
}, []);
// 再来一个副作用,用于获取跳转到 Home 组件时传递的 state 数据
useEffect(() => {
if (location.state) {
setAlert(location.state)
}
}, [location])
const showAlert = alert ?
| 姓名 | 年龄 | 联系方式 | 操作 |
|---|