0

I have created app using create-react-app. I import owl.carousel but why it is showing this error.

how to define this $

  Line 12:15:  '$' is not defined  no-undef
  Line 21:5:   '$' is not defined  no-undef
  Line 24:5:   '$' is not defined  no-undef

Here is the Component Code


    var owl = $(".owl-carousel");
    owl.owlCarousel({
      items: 4,
      loop: true,
      margin: 10,
      autoplay: true,
      autoplayTimeout: 1000,
      autoplayHoverPause: true,
    });
    $(".play").on("click", function () {
      owl.trigger("play.owl.autoplay", [1000]);
    });
    $(".stop").on("click", function () {
      owl.trigger("stop.owl.autoplay");
    });

Cannot read property of undefined. this picture of error that I got

3
  • Have you searched for jquery? Commented Sep 12, 2020 at 3:08
  • Not answering the question, but it cannot be a good idea to combine jquery with react. It is better to use react refs => reactjs.org/docs/refs-and-the-dom.html Commented Sep 12, 2020 at 3:20
  • @lanxion OP isn't the one combining it, but rather the owl module they're using is, and jquery appears to be a peer dependency. Commented Sep 12, 2020 at 3:21

1 Answer 1

1

Import jQuery to your project with ReactJS like so...

import $ from 'jquery';

Before you can do that, you must install it to your project. See the NPM documentation for more info: npm->jQuery. You can install it with npm by means of...

npm install --save jquery

And you can know the installation worked by checking your package.json file and seeing...

"dependencies": {
  "jquery": "^3.3.1",
  ....
Sign up to request clarification or add additional context in comments.

1 Comment

Also, according to npmjs.com/package/react-owl-carousel, you are required to do some webpack config to inject a global window.jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.