我剛剛開始使用 React。我遇到了這樣一個問題:如何在 useEffect 中制作它,以便在執行第一個請求后,從中獲取接收到的資料并將其發送到第二個(跟隨它)?提前致謝。
const FullSizeWorkSchedule = () => {
const [events, setEvents] = useState([])
const [user, setUser] = useState([])
const [EventsListActive, setEventsListActive] = useState(true)
useEffect(() => {
const fetch2 = fetch('/profile')
.then(res => res.json())
.then(user => {setUser(user)});
// Send example user state
const fetch1 = fetch('/events')
.then(res => res.json())
.then(events => {setEvents(events)});
},[])
uj5u.com熱心網友回復:
我個人會為此使用 Axios,但這里嘗試完成你想要的:
useEffect(() => {
async function fetchData() {
const res1 = await fetch("/events");
const events = res1.json();
setEvents(events);
const res2 = await fetch("/user", { body: events });
const userData = res2.json();
setUser(userData);
}
fetchData();
}, []);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/506840.html