0

В консоли просит передать в аргумент массив, что я, вроде как, и делаю, так же что queryKey undefined. Подскажите, в чем проблема? В упор ее не вижу..

Ошибки в консили: As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']

Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ["posts",1]

import {useEffect, useState} from "react";
import {keepPreviousData, useQuery, useQueryClient} from '@tanstack/react-query'

import {Body, BtnWrapper, Button, Container, Title, Wrapper} from '../../utils/styledComponents.jsx'

const Posts = () => {
    const [page, setPage] = useState(1);

    const apiUrl = `https://jsonplaceholder.typicode.com/posts?page=${page}`
    const fetchPosts = async () => {
        await fetch(apiUrl)
            .then(res => res.json())
    }

    const {data, isLoading} = useQuery({
        queryKey: ['posts', page],
        queryFn: fetchPosts,
        placeholderData: keepPreviousData
    })

    const queryClient = useQueryClient()
    useEffect(() => {
        queryClient.prefetchQuery(['posts', page], fetchPosts)
    }, [page]);

    const nextPage = () => {
        setPage(prev => prev + 1)
    }

    const prevPage = () => {
        setPage(prev => prev - 1)
    }
1

0

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Посмотрите другие вопросы с метками или задайте свой вопрос.