Home  >  Q&A  >  body text

keen-slider is not compatible with data fetching functions

<p>I want to get public data and display them using keen-slider. However, it doesn't seem to work very well, especially when I try to add left and right arrows. They won't show up. Another problem is that the slider freezes when first loading and only moves after resizing the window. </p> <pre class="brush:php;toolbar:false;">const [data, setData] = useState([]); const [options, setOptions] = useState({}); const [currentSlide, setCurrentSlide] = useState(0); const [loaded, setLoaded] = useState(false); const [sliderRef, instanceRef] = useKeenSlider(options); useEffect(() => { fetch("./data.json") .then((response) => response.json()) .then((json) => { console.log(json); setData(json); }); setOptions({ initial: 0, slides: { origin: "center", perView: "3", spacing: 25 }, slideChanged(slider) { setCurrentSlide(slider.track.details.rel); }, created() { setLoaded(true); } }); }, []);</pre> <p>See the full code [here](https://codesandbox.io/s/delicate-butterfly-d4vp5k?file=/src/App.js). </p> <p>I followed the advice in these posts 1 post 2 and updated the options in <code>useKeenSlider</code> when getting the data. This helps alleviate the error message, but does not completely solve the problem. </p> <p>I also added arrows following the example here. </p> <p>Any suggestions are welcome! </p>
P粉208286791P粉208286791403 days ago485

reply all(1)I'll reply

  • P粉268284930

    P粉2682849302023-08-16 16:16:33

    I think your mapping data failed, all the results are:

    <div className="keen-slider__slide number-slide1">1</div>
    But the result we expect is:

    <div className="keen-slider__slide number-slide1">1</div>
          <div className="keen-slider__slide number-slide2">2</div>
          <div className="keen-slider__slide number-slide3">3</div>
          <div className="keen-slider__slide number-slide4">4</div>
          <div className="keen-slider__slide number-slide5">5</div>
          <div className="keen-slider__slide number-slide6">6</div>

    reply
    0
  • Cancelreply