0

how can I use jquery in react js

<script>
    $('#demo').daterangepicker({
        "startDate": "01/21/2023",
        "endDate": "01/27/2023"
    }, function(start, end, label) {
      console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
    });
</script>
Robin Hood
  • 710
  • 3
  • 16
DP Patel
  • 3
  • 3

1 Answers1

0

Go to your project folder via terminal and run these commands

npm install jquery --save
npm i --save-dev @types/jquery

Use import $ from jquery into your JSX file where you need to use. your main.jsx file will be like this

import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';


//   react code here


$('#demo').daterangepicker({
    "startDate": "01/21/2023",
    "endDate": "01/27/2023"
}, function(start, end, label) {
  console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
});

// react code here

and your index.html will be like this

<!DOCTYPE html>
<html>
<head>
    <script src="main.jsx"></script>
</head>
<body>
    //your code
</body>
</html>

Reference

Robin Hood
  • 710
  • 3
  • 16