<bdo id='Of33N'></bdo><ul id='Of33N'></ul>
<tfoot id='Of33N'></tfoot>

  • <small id='Of33N'></small><noframes id='Of33N'>

    <legend id='Of33N'><style id='Of33N'><dir id='Of33N'><q id='Of33N'></q></dir></style></legend>
      <i id='Of33N'><tr id='Of33N'><dt id='Of33N'><q id='Of33N'><span id='Of33N'><b id='Of33N'><form id='Of33N'><ins id='Of33N'></ins><ul id='Of33N'></ul><sub id='Of33N'></sub></form><legend id='Of33N'></legend><bdo id='Of33N'><pre id='Of33N'><center id='Of33N'></center></pre></bdo></b><th id='Of33N'></th></span></q></dt></tr></i><div id='Of33N'><tfoot id='Of33N'></tfoot><dl id='Of33N'><fieldset id='Of33N'></fieldset></dl></div>

        GlideJS - 使用带有 React 组件的幻灯片时的奇怪行为

        时间:2023-09-07
          <tbody id='eHiiG'></tbody>
      1. <small id='eHiiG'></small><noframes id='eHiiG'>

          <legend id='eHiiG'><style id='eHiiG'><dir id='eHiiG'><q id='eHiiG'></q></dir></style></legend>
          <i id='eHiiG'><tr id='eHiiG'><dt id='eHiiG'><q id='eHiiG'><span id='eHiiG'><b id='eHiiG'><form id='eHiiG'><ins id='eHiiG'></ins><ul id='eHiiG'></ul><sub id='eHiiG'></sub></form><legend id='eHiiG'></legend><bdo id='eHiiG'><pre id='eHiiG'><center id='eHiiG'></center></pre></bdo></b><th id='eHiiG'></th></span></q></dt></tr></i><div id='eHiiG'><tfoot id='eHiiG'></tfoot><dl id='eHiiG'><fieldset id='eHiiG'></fieldset></dl></div>
            <bdo id='eHiiG'></bdo><ul id='eHiiG'></ul>

                  <tfoot id='eHiiG'></tfoot>
                  本文介绍了GlideJS - 使用带有 React 组件的幻灯片时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将 GlideJS 与 React 项目一起使用,但它返回了一个奇怪的行为.组件没有在每个视图中显示一个,并且它们的宽度已更改.

                  I'm using GlideJS with a React Project, but it's returning a strange behavior. The components are not showing one per view and the width of them is changed.

                  组件:

                  import React, { Component } from "react";
                  import Glide from "@glidejs/glide";
                  
                  export default class SliderGlide extends Component {
                    state = { id: null };
                  
                    componentDidMount = () => {
                      // Generated random id
                      this.setState(
                        { id: `glide-${Math.ceil(Math.random() * 100)}` },
                        this.initializeGlider
                      );
                    };
                  
                    initializeGlider = () => {
                      this.slider = new Glide(`#${this.state.id}`, this.props.options);
                      this.slider.mount();
                    };
                  
                    componentWillReceiveProps = newProps => {
                      if (this.props.options.startAt !== newProps.options.startAt) {
                        this.slider.go(`=${newProps.options.startAt}`);
                      }
                    };
                  
                    render = () => (
                      <div
                        id={this.state.id}
                        className="mt-10"
                        style={{ overflowX: "hidden", userSelect: "none", maxWidth: "100vw" }}
                      >
                        <div className="glide__arrows" data-glide-el="controls">
                          <button
                            className="glide__arrow glide__arrow--left rounded-full"
                            data-glide-dir="<"
                            title="Veja mais ofertas!"
                          >
                            <span className="hidden">Anterior</span>
                          </button>
                          <button
                            className="glide__arrow glide__arrow--right rounded-full"
                            data-glide-dir=">"
                            title="Veja mais ofertas!"
                          >
                            <span className="hidden">Próximo</span>
                          </button>
                        </div>
                        <div className="glide__track" data-glide-el="track">
                          <div className="glide__slides" style={{ display: "flex" }}>
                            {this.props.children.map((slide, index) => {
                              return React.cloneElement(slide, {
                                key: index,
                                className: `${slide.props.className} glide__slide`
                              });
                            })}
                          </div>
                        </div>
                        <div className="glide__bullets" data-glide-el="controls[nav]">
                          {this.props.children.map((slide, index) => {
                            return <button key={index} className="glide__bullet rounded-full" data-glide-dir={"=" + index} />;
                          })}
                        </div>
                      </div>
                    );
                  }
                  
                  SliderGlide.defaultProps = {
                    options: {}
                  };
                  

                  所以在 Carousel 组件内部,我传递了子组件和 glide 选项,它们是组件.

                  const Plans = ({ plans, handleOffer }) => { const carouselOptions = { type: 'slide', perView: 1, startAt: 0, } return ( <div className="section__slider relative mt-10 flex justify-center items-center"> <Carousel options={carouselOptions}> { plans.map((plan, i) => { return ( <OfferProduct key={i} i={i} plan={plan} handleOffer={handleOffer}/> ) }) } </Carousel> </div> ) } export default Plans;

                  I want to know if the problem is related to my code, some missed styledsheet that i need to import or if the actions passed to glide are wrong. 

                  我想知道问题是否与我的代码、我需要导入的一些丢失的样式表有关,或者传递给 glide 的操作是否错误.

                  解决方案

                  推荐答案

                  我刚刚使用 glidejsReact 为您设置了完整的工作演示.检查一下,让我知道它是否适合你.

                  index.js

                  class Plans extends Component {
                    state = {
                      myPlans: [
                        { id: 0, text: "plan 0", price: 0 },
                        { id: 1, text: "plan 1", price: 1 },
                        { id: 2, text: "plan 2", price: 2 },
                        { id: 3, text: "plan 3", price: 3 }
                      ]
                    };
                    handleOffer = id => {
                      console.log("handleOffer clicked, id: ", id);
                    };
                  
                    render() {
                      const carouselOptions = { type: "slide", perView: 1, startAt: 0 };
                  
                      return (
                        <div className="home-section test">
                          <SliderGlide options={carouselOptions}>
                            {this.state.myPlans.map(plan => (
                              <OfferProduct
                                key={plan.id}
                                plan={plan}
                                handleOffer={this.handleOffer}
                              />
                            ))}
                          </SliderGlide>
                        </div>
                      );
                    }
                  }
                  export default Plans;
                  

                  OfferProduct.js

                  const OfferProduct = ({ plan, handleOffer }) => {
                    return (
                      <>
                        <div onClick={() => handleOffer(plan.id)} className="card">
                          <p>
                            <h3> Card no: {plan.id} </h3>
                            <span>price: {plan.price}</span>
                          </p>
                        </div>
                      </>
                    );
                  };
                  
                  export default OfferProduct;
                  

                  SliderGlide.js

                  export default class SliderGlide extends Component {
                    state = { id: null };
                  
                    componentDidMount = () => {
                      // Generated random id
                      this.setState(
                        { id: `glide-${Math.ceil(Math.random() * 100)}` },
                        this.initializeGlider
                      );
                    };
                  
                    initializeGlider = () => {
                      this.slider = new Glide(`#${this.state.id}`, this.props.options);
                      this.slider.mount();
                    };
                  
                    componentWillReceiveProps = newProps => {
                      if (this.props.options.startAt !== newProps.options.startAt) {
                        this.slider.go(`=${newProps.options.startAt}`);
                      }
                    };
                  
                    render = () => (
                      // controls
                      <div id={this.state.id} className="slider">
                        <div className="two-controls-btns" data-glide-el="controls">
                          <button className="arrow-left" data-glide-dir="<" title="start">
                            <span className="hidden">Start</span>
                          </button>
                          <button className="arrow-right" data-glide-dir=">" title="end">
                            <span className="hidden">End</span>
                          </button>
                        </div>
                        {/* track  *

                    <tbody id='6VTCp'></tbody>
                1. <tfoot id='6VTCp'></tfoot>

                  <i id='6VTCp'><tr id='6VTCp'><dt id='6VTCp'><q id='6VTCp'><span id='6VTCp'><b id='6VTCp'><form id='6VTCp'><ins id='6VTCp'></ins><ul id='6VTCp'></ul><sub id='6VTCp'></sub></form><legend id='6VTCp'></legend><bdo id='6VTCp'><pre id='6VTCp'><center id='6VTCp'></center></pre></bdo></b><th id='6VTCp'></th></span></q></dt></tr></i><div id='6VTCp'><tfoot id='6VTCp'></tfoot><dl id='6VTCp'><fieldset id='6VTCp'></fieldset></dl></div>

                    <bdo id='6VTCp'></bdo><ul id='6VTCp'></ul>
                      1. <legend id='6VTCp'><style id='6VTCp'><dir id='6VTCp'><q id='6VTCp'></q></dir></style></legend>

                        <small id='6VTCp'></small><noframes id='6VTCp'>