Comments (8)
Thanks for the quick reply. Do you think the library should ideally detect and handle this automatically?
from keen-slider.
@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.
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.
Yes, I plan to implement a mutation observer.
from keen-slider.
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.
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.
You should follow the suggestion of @yogeshkotadiya.
from keen-slider.
@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)
- Mobile drag issues HOT 2
- 【Feature】can play audio sound like html native select option element when using Timepicker ?
- Vertical auto-switch unexpected behavior after several seconds HOT 1
- Drag deceleration speed
- Stop moveToIdx move backward on loop
- I wonder why there's such a blank area
- [React] Stop auto switch after navigation
- Lazy loading when slides.perView > 1
- scrolling issue on mobile with scroll variant of "data-keen-slider-scrollable".
- SyntaxError: Named export 'useKeenSlider' not found.
- [Angular] ReferenceError: document is not defined
- Keen-slider doesn't work properly with vue-transition.
- Slider Switches Slide After Very Short Drag
- Click while sliding with free-snap mode HOT 1
- [VanillaJS][v5] translate3d values for slides are sometimes wrong
- How to avoid skipping slides when mouse scrolling? HOT 1
- moveToIdx does not work in certain conditions HOT 3
- Dynamic content makes slides jumping between loops
- Scrolling to a Specific Element in a Multi-Element Slide
- Weird Resize Happening to All Slides HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keen-slider.