NodeJS 为什么不能直接使用组件?

zpgglvta  于 2022-12-22  发布在  Node.js
关注(0)|答案(2)|浏览(113)

我尝试在SwiperSlide中使用我的组件:直接在里面使用它确实有效:

<Swiper>
          <SwiperSlide>
            <img src="https://swiperjs.com/demos/images/nature-1.jpg" />
          </SwiperSlide>
</Swiper>

但如果你这样做:

const Foo = () =>
  <SwiperSlide>
    <img src="https://swiperjs.com/demos/images/nature-3.jpg" />
  </SwiperSlide>
;

<Swiper>
         <Foo />
</Swiper>

不管用。后来我发现这个管用:

<Swiper>
         {Foo()}
</Swiper>

但是如果可能的话,我会正确地修复和使用<Foo />
完整代码:

import React, { useRef, useState } from "react";
    // Import Swiper React components
    import { Swiper, SwiperSlide } from "swiper/react";
    
    // Import Swiper styles
    import "swiper/css";
    import "swiper/css/pagination";
    import "swiper/css/navigation";
    import "swiper/css/navigation";
    import "swiper/css/effect-fade";
    
    import "./styles.css";
    
    // import required modules
    import { EffectFade, Autoplay, Pagination, Navigation } from "swiper";
    
    export default function App() {
      return (
        <>
          <Swiper
            effect={"fade"}
            loop={true}
            navigation={true}
            spaceBetween={30}
            centeredSlides={true}
            autoplay={{
              delay: 5000,
              disableOnInteraction: false,
              pauseOnMouseEnter: true
            }}
            pagination={{
              clickable: true
            }}
            modules={[EffectFade, Autoplay, Pagination, Navigation]}
            className="mySwiper"
          >
            <SwiperSlide>
              <img src="https://swiperjs.com/demos/images/nature-1.jpg" />
            </SwiperSlide>
            <Foo />
          </Swiper>
        </>
      );
    }

const Foo = () => (
  <SwiperSlide>
    <img src="https://swiperjs.com/demos/images/nature-2.jpg" />
  </SwiperSlide>
);

实时示例here
我错过了什么?我该怎么弥补?

5rgfhyps

5rgfhyps1#

这是因为Foo不是一个合适的jsx组件。并将自定义组件的显示名称设置为SwiperSlider。重写如下。
基于在以下位置找到的解决方案:https://github.com/nolimits4web/swiper/issues/4413

const Foo = () => {
    return (
      <SwiperSlide>
        <img src="https://swiperjs.com/demos/images/nature-2.jpg" />
      </SwiperSlide>
    );
  };
  Foo.displayName = "SwiperSlide";
  return (
    <>
      <Swiper
        effect={"fade"}
        loop={true}
        navigation={true}
        spaceBetween={30}
        centeredSlides={true}
        BannerSliderSlide={"Foo"}
        autoplay={{
          delay: 5000,
          disableOnInteraction: false,
          pauseOnMouseEnter: true
        }}
        pagination={{
          clickable: true
        }}
        modules={[EffectFade, Autoplay, Pagination, Navigation]}
        className="mySwiper"
      >
        <Foo />
      </Swiper>
    </>

代码沙盒:https://codesandbox.io/s/swiper-autoplay-react-forked-xd8xgd?file=/src/App.jsx:467-1177

woobm2wo

woobm2wo2#

Swiper可能需要SwiperSlide作为Swiper的子元素。您的代码在VDOM中的这两个元素之间注入了一个中间元素<Foo />。我不确定如何进行抽象,但您可能需要在每个项上添加SwiperSlide组件,并使用查尔兹元素进行抽象

相关问题