简体   繁体   English

如何在 useEffect 中将结果从一个查询发送到另一个查询?

[英]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.

相关问题 如何在另一个组件 useEffect 中设置状态? - How can I setState in a another components useEffect? 如何在 useEffect 中使用异步 function 的结果? - How can I use the results of an async function inside useEffect? 在MERN反应中,我如何将JSON数据从一个组件发送到另一个组件,以及如何获取它 - in MERN react, how i can send JSON data from one component to another and how to fetch it 如何避免从另一个 useEffect() 触发 useEffect()? - How to avoid triggering useEffect() from another useEffect()? 如何在 React 中不调用该组件的情况下将数据从一个组件发送到另一个组件? - How can I send data from one component to another without calling that component in React? 如何将变量的值从一个组件发送到另一个组件? (反应) - How can I send the value of a variable from one component to another? (React) 如何使用 Firebase 实时数据库将信息从一个用户发送到另一个用户? - How can I send information from one user to another using Firebase realtime database? 如何在初始页面加载时将 state 从一个 useEffect 传递到另一个 useEffect? - How to pass state from one useEffect to another useEffect on intial page load? 如何同步来自两个不同查询的数据,其中一个在 useEffect 中进行转换? - How can I synchronize data from two different queries, where one is being transformed in useEffect? 如何将数据传输到另一个组件(useEffect 问题) - how can I transfer the data to another component (useEffect problem)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM