reactjsglidejs

Glide JS with react


followed the documentation and got the following error: TypeError: Cannot read property 'querySelector' of undefined. I have added the following to my index.html

<link
      rel="stylesheet"
      href="node_modules/@glidejs/glide/dist/css/glide.core.min.css"
    /> 

for some reason glide breaks my entire application. Is there another step I need to follow in order to get GlideJS to work in a create-react-app? any suggestions would help a lot. Thanks! my code is as follows:

import React from "react"
import Glide from "@glidejs/glide"

const Test = () => {
  new Glide(".glide").mount()
  return (
    <>
      {" "}
      <div className='glide'>
        <div className='glide__track' data-glide-el='track'>
          <ul className='glide__slides'>
            <li className='glide__slide slider'>1</li>
            <li className='glide__slide slider'>2</li>
            <li className='glide__slide slider'>3</li>
            <li className='glide__slide slider'>4</li>
          </ul>
        </div>
        <div className='glide__arrows' data-glide-el='controls'>
          <button
            className='glide__arrow glide__arrow--prev'
            data-glide-dir='<'
          >
            prev
          </button>
          <button
            className='glide__arrow glide__arrow--next'
            data-glide-dir='>'
          >
            next
          </button>
        </div>
      </div>
    </>
  )
}

export default Test



Solution

  • Use useEffect to mimick componentDidMount(). And mount the slider inside of useEffect()

    Try something like this:

    import React, { useEffect } from "react"
    import Glide from "@glidejs/glide"
    
    // Using SCSS
    import "./styles.scss"
    
    const sliderConfiguration= {
      gap: 20,
      perView: 2,
      startAt: 0,
      type: "slider"
    };
    
    const Test = () => {
      const slider = new Glide('.glide', sliderConfiguration);
    
      useEffect(() => {
        return () => slider.mount()
      }, [slider])
    
      return (
        <>
          {" "}
          <div className='glide'>
            <div className='glide__track' data-glide-el='track'>
              <ul className='glide__slides'>
                <li className='glide__slide slider'>1</li>
                <li className='glide__slide slider'>2</li>
                <li className='glide__slide slider'>3</li>
                <li className='glide__slide slider'>4</li>
              </ul>
            </div>
            <div className='glide__arrows' data-glide-el='controls'>
              <button
                className='glide__arrow glide__arrow--prev'
                data-glide-dir='<'
              >
                prev
              </button>
              <button
                className='glide__arrow glide__arrow--next'
                data-glide-dir='>'
              >
                next
              </button>
            </div>
          </div>
        </>
      )
    }
    
    export default Test
    

    styles.scss

    // Required Core Stylesheet
    @import "node_modules/@glidejs/glide/src/assets/sass/glide.core";
    

    Note: If your slide items using a dynamic data, you should pass variable that contain the data, to useEffect() dependencies.