Home > Web Front-end > Front-end Q&A > Can jquery be embedded in react?

Can jquery be embedded in react?

WBOY
Release: 2022-04-21 16:34:34
Original
2622 people have browsed it

jquery can be embedded in react. Method: 1. Use "npm install jquery --save" to install jquery; 2. Modify the webpack configuration file; 3. Use "require (jquery.plugin path)" to reference the jquery plug-in.

Can jquery be embedded in react?

The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.

Can jquery be embedded in react

Can jquery be embedded in react

First of all, jquery must be installed in react

npm install jquery --save
Copy after login

The $ variable is mounted under the window. You can use $ directly in the project without citing it.

     //修改webpack配置文件:
     plugins:[
        new webpack.ProvidePlugin({
        $:"jquery",
        jQuery:"jquery",
        "window.jQuery":"jquery"
        })
      ]
Copy after login

How to use the jquery plug-in

First use require(/your/path/jquery.plugin) Quoting the jquery plug-in

webpack supports ES6's import, requirejs, and commonjs syntax, which can be referenced using CMD,

AMD.

AMD writing method:

     define(["jquery"],function($){
         ...
         var initialChart = function(data){
             //插件逻辑
         }
         ...
         $(function(){
             //页面逻辑
         })
         ...
         
         return{
            initialChart:initialChart //导出函数
         } 
     })
Copy after login

CMD writing method

 function orgOrgChart(data){
          //插件逻辑
      }
      $(function(){
             //页面逻辑
      })
      module.exports.orgOrgChart = orgOrgChart //导出函数
Copy after login

Finally quote the exported function in react and call it in the life cycle function

     import {initialChart} from '../../es5Components/emp-orgChart.js' 
     import {orgOrgChart} from '../../es5Components/emp-orgChart.js' 
     
     ...
     componentDidMount(){
         initialChart(this.state.data);
         orgOrgChart(this.state.data)
     }
     ....
Copy after login

Recommended learning: "react video tutorial"

The above is the detailed content of Can jquery be embedded in react?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template