Home > database >  How can I send results from one query to another in useEffect?
How can I send results from one query to another in useEffect?

Time:09-12

I'm just getting started with 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)? 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)});


    },[])

CodePudding user response:

I'd personally use Axios for that but here is an attempt to accomplish what you want:

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();
}, []);
  • Related