Git Product home page Git Product logo

Comments (8)

x4th avatar x4th commented on August 26, 2024 1

Thanks for the quick reply. Do you think the library should ideally detect and handle this automatically?

from keen-slider.

yogeshkotadiya avatar yogeshkotadiya commented on August 26, 2024 1

@x4th As a workaround, don't initialize the slider until you get the data.

Or you can manually refresh as @rcbyr Suggested.

import * as React from "react";
import "./styles.css";
import { useKeenSlider } from "keen-slider/react";
import "keen-slider/keen-slider.min.css";

const target_images = [
  "https://images.unsplash.com/photo-1590004953392-5aba2e72269a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80",
  "https://images.unsplash.com/photo-1590004845575-cc18b13d1d0a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80",
  "https://images.unsplash.com/photo-1590004987778-bece5c9adab6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80",
  "https://images.unsplash.com/photo-1590005176489-db2e714711fc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80"
];

const Slider = ({images}: {images:any[]}) => {
  const [ref] = useKeenSlider<HTMLDivElement>();

  return (
    <div ref={ref} className="keen-slider">
    {images.map((src, idx) => (
      <div key={idx} className="keen-slider__slide">
        <img src={src} />
      </div>
    ))}
  </div>
  )
}

export default function App() {
  const [images, setImages] = React.useState();
  React.useEffect(() => {
    setTimeout(() => setImages(target_images), 1000);
  },[]);

  return (
    <React.Fragment>
      {Array.isArray(images) && (
        <Slider images={images} />
      )}
    </React.Fragment>
  );
}

from keen-slider.

rcbyr avatar rcbyr commented on August 26, 2024

Hey @x4th

you only need to change the size of the slider manually after loading the images is complete.

See here: https://codesandbox.io/s/keen-slider-default-react-ts-k7wir?file=/src/App.tsx:975-1008

from keen-slider.

rcbyr avatar rcbyr commented on August 26, 2024

Yes, I plan to implement a mutation observer.

from keen-slider.

x4th avatar x4th commented on August 26, 2024

That's great to hear.

I've run into another issue which I'd like to ask you about and maybe it'll help someone out in the future as well. I'm trying to make the zoom out slider example work for me when again, I'm fetching image urls from the server and then the slides length isn't available immediatelly. Is there a way to make this work?

from keen-slider.

rcbyr avatar rcbyr commented on August 26, 2024

Currently, you can only change the slides with a refresh. See here: https://codesandbox.io/s/keen-slider-zoomout-react-69dsk?file=/src/App.js

from keen-slider.

rcbyr avatar rcbyr commented on August 26, 2024

You should follow the suggestion of @yogeshkotadiya.

from keen-slider.

azhararmar avatar azhararmar commented on August 26, 2024

@x4th As a workaround, don't initialize the slider until you get the data.

Or you can manually refresh as @rcbyr Suggested.

import * as React from "react";
import "./styles.css";
import { useKeenSlider } from "keen-slider/react";
import "keen-slider/keen-slider.min.css";

const target_images = [
  "https://images.unsplash.com/photo-1590004953392-5aba2e72269a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80",
  "https://images.unsplash.com/photo-1590004845575-cc18b13d1d0a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80",
  "https://images.unsplash.com/photo-1590004987778-bece5c9adab6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80",
  "https://images.unsplash.com/photo-1590005176489-db2e714711fc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&h=500&w=800&q=80"
];

const Slider = ({images}: {images:any[]}) => {
  const [ref] = useKeenSlider<HTMLDivElement>();

  return (
    <div ref={ref} className="keen-slider">
    {images.map((src, idx) => (
      <div key={idx} className="keen-slider__slide">
        <img src={src} />
      </div>
    ))}
  </div>
  )
}

export default function App() {
  const [images, setImages] = React.useState();
  React.useEffect(() => {
    setTimeout(() => setImages(target_images), 1000);
  },[]);

  return (
    <React.Fragment>
      {Array.isArray(images) && (
        <Slider images={images} />
      )}
    </React.Fragment>
  );
}

This seems to be a good solution and it worked for me.

from keen-slider.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.