简体   繁体   English

用于 React 的 Swiper js 无法正确渲染子级

[英]Swiper js for React does not render children properly

First of all, I use React, Gatsby and Swiperjs.首先,我使用 React、Gatsby 和 Swiperjs。

I have problem with rendering children into Swiper component.我在将孩子渲染到Swiper组件时遇到问题。 I use swiper in two places (Blog and Portfolio sections) so I created CardsSlider component to reuse it.我在两个地方(博客和投资组合部分)使用了 swiper,所以我创建了CardsSlider组件来重用它。 I use two types of similar components so I use Higher Order Component to surround each card component with SwiperSlide .我使用了两种类似的组件,所以我使用高阶组件用SwiperSlide包围每个卡片组件。 Then I iterate through each data to generate cards surrounded with SwiperSlide component as children prop for CardsSlider which is basically Swiper .然后我遍历每个数据以生成被SwiperSlide组件包围的卡片,作为Swiper的子道具,它基本上是CardsSlider Problem is: SwiperSlide cards are not wrapped with SwiperWrapper component what you can see in ss of devtools down below.问题是: SwiperSlide卡没有用 SwiperWrapper 组件包装,您可以在下面的 devtools 的 ss 中看到。

HTML Tags HTML标签

Blog component:博客组件:

const Blog = () => {
  const { postImage } = useStaticQuery(...)
  const CardsData = [
    {
      title: 'Post1',
      description:
        'Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum veritatis in reiciendis sit facere enim modi, libero placeat consectetur eaque?',
      image: postImage,
    }, 
     ...
  ];

  const BlogPostCardWithSlider = withSlider(BlogPostCard);

  return (
    <StyledSection>
      <StyledHeader text="BLOG" />
      <CardsSlider pagination>
        {CardsData.map((data) => (
          <BlogPostCardWithSlider key={data.title} data={data} />
        ))}
      </CardsSlider>
      <Button dark text="ZOBACZ WSZYSTKIE WPISY" />
    </StyledSection>
  );
};

export default Blog;

Portfolio component:投资组合组件:

 const Portfolio = () => {
  const { projectImage } = useStaticQuery(...);

  const CardsData = [
    {
      title: 'Project1',
      description:
        'Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum veritatis in reiciendis sit facere enim modi, libero placeat consectetur eaque?',
      image: projectImage,
      techStack: ['bootstrap', 'nodejs', 'sequelize', 'react', 'mssql'],
    },
    ...
  ];

  const ProjectCardWithSlider = withSlider(ProjectCard);

  return (
    <StyledSection odd>
      <StyledHeader text="PORTFOLIO" />
      <CardsSlider navigation loop>
        {CardsData.map((data) => (
          <ProjectCardWithSlider key={data.title} data={data} />
        ))}
      </CardsSlider>
    </StyledSection>
  );
};

export default Portfolio;

withSlider HOC:带有滑块 HOC:

import React from 'react';
import { SwiperSlide } from 'swiper/react';

const slideStyles = {
  display: 'flex',
  justifyContent: 'center',
  alignItems: 'center',
};

const withSlider = (WprappedComponent) => (props) => (
  <SwiperSlide style={slideStyles}>
    <WprappedComponent {...props} />
  </SwiperSlide>
);

export default withSlider;

CardsSlider component: CardsSlider 组件:

import React from 'react';
import styled from 'styled-components';
import { Swiper } from 'swiper/react';
import SwiperCore, { Pagination, Navigation } from 'swiper';
import 'swiper/swiper-bundle.css';

SwiperCore.use([Pagination, Navigation]);

const SwiperWrapper = styled(Swiper)`
  ...
`;

const CardsSlider = ({ children, pagination, ...rest }) => {
  const params = {
    spaceBetween: 0,
    pagination: pagination && { clickable: true },
    ...rest,
  };

  return <SwiperWrapper {...params}>{children}</SwiperWrapper>;
};

Fun fact is, when i map children like this:有趣的事实是,当我像这样的 map 孩子时:

{children.map((child) => <SwiperSlide>{child}<SwiperSlide/>)}

It render in correct place.它呈现在正确的位置。

Thank you for help.谢谢你的帮助。

I have the same exact problem when dealing with a minimum example.在处理最小示例时,我遇到了同样的问题。 Seems the children are added AFTER the wrapper, if they are not SwiperSlides.似乎孩子们是在包装之后添加的,如果他们不是 SwiperSlides。 My suggestion is to keep SwiperSlide as direct children of Swiper component, and use HOC for the content of the slides.我的建议是让 SwiperSlide 作为 Swiper 组件的直接子级,并使用 HOC 作为幻灯片的内容。 At least, seems the only thing working for me, and i'm fine with it as i dont want to lose much time on the internals of Swiper.至少,这似乎是唯一对我有用的东西,而且我很好,因为我不想在 Swiper 的内部结构上浪费太多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM