@laravel_creative_3103

Возникает ошибка в RTK QUERY, React, в чем может быть ошибка?

const AllServices = () => {
    const navigationPrevRef = React.useRef(null);
    const navigationNextRef = React.useRef(null);
    const { data: popularServies } = useGetPopularServicesQuery();
    const { data: organizations } = useGetOrganizationsQuery();

    const toShowServices = (id: string) => {
        console.log({ id });
        const { data } = useGetServicesByOrgationzationQuery(id); // при клике возникает такая ошибка react-dom.development.js:14906 Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
        console.log('check:', data);
    };

    console.log({ organizations });

    return (
        <div style={{ margin: '0 auto', maxWidth: '1440px' }}>
            <TitleWithSearch title='Услуги' placeholder='Найдите услуги' />
            <div className='slider-suppliers' style={{ marginTop: '80px' }}>
                <Row justify={'space-between'}>
                    <div className='prevSlider' ref={navigationPrevRef}>
                        <img src={prevSlider} alt='' />
                    </div>
                    <div className='nextSlider' ref={navigationNextRef}>
                        <img src={nextSlider} alt='' />
                    </div>
                    <Swiper
                        style={{ marginTop: '20px' }}
                        // @ts-ignore
                        slidesPerView={1}
                        spaceBetween={10}
                        pagination={{
                            clickable: true,
                        }}
                        breakpoints={{
                            640: {
                                slidesPerView: 5,
                                spaceBetween: 50,
                            },
                        }}
                        navigation={{
                            prevEl: navigationPrevRef.current,
                            nextEl: navigationNextRef.current,
                        }}
                        modules={[A11y, Scrollbar, Navigation, Pagination]}
                        onBeforeInit={(swiper) => {
                            if (typeof swiper.params.navigation === 'object') {
                                swiper.params.navigation.prevEl = navigationPrevRef.current;
                                swiper.params.navigation.nextEl = navigationNextRef.current;
                            }
                        }}
                        className='mySwiper'
                    >
                        {organizations?.data.map((item: any) => (
                            <SwiperSlide
                                style={{ cursor: 'pointer' }}
                                key={item?.id}
                                onClick={(e) => {
                                    e.stopPropagation();
                                    toShowServices(item.id);
                                }}
                            >
                                {item?.name}
                            </SwiperSlide>
                        ))}
                    </Swiper>
                </Row>
            </div>

            <div className='services-block'>
                <div className='cards-services'>
                    {popularServies?.map((item) => (
                        <ItemService key={item.id} service={item} />
                    ))}
                </div>
            </div>
        </div>
    );
};

export default AllServices;


Как можно было бы переформулировать функцию?
  • Вопрос задан
  • 46 просмотров
Пригласить эксперта
Ответы на вопрос 1
@karminski
Senior React.JS Developer
В тексте ошибки все четко описано - хуки нельзя использовать внутри тела функции. Только сверху компонента (как у вас выше) или внутри другого хука.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы