[英]How can I send results from one query to another in useEffect?
I'm just getting started with React.我刚刚开始使用 React。 And I ran into such a problem: how in useEffect can I make it so that after the first request is executed, take the received data from it and send it to the second one (following it)?
我遇到了这样一个问题:如何在 useEffect 中制作它,以便在执行第一个请求后,从中获取接收到的数据并将其发送到第二个(跟随它)? Thanks in advance.
提前致谢。
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)});
},[])
I'd personally use Axios for that but here is an attempt to accomplish what you want:我个人会为此使用 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();
}, []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.