如何在React中集成jQuery插件

时间:2017-06-23 23:28:30

标签: javascript jquery reactjs webpack jquery-ui-plugins

我想在React应用中使用https://github.com/t1m0n/air-datepicker,但它不起作用。



import React from 'react';
import AirDatepicker from 'air-datepicker';

class Datepicker extends React.Component {
  render() {
    return(
      <AirDatepicker />
    )
  }
}

export default Datepicker;
`
&#13;
<script src="./../bower_components/jquery/dist/jquery.min.js"></script>
&#13;
&#13;
&#13;

这会产生:

error($ is not defined)

另一种方法:

&#13;
&#13;
import React from 'react';
import $ from 'jquery';
import AirDatepicker from 'air-datepicker';

class Datepicker extends React.Component {
  render() {
    return(
      <AirDatepicker />
    )
  }
}

export default Datepicker;
&#13;
&#13;
&#13;

同样的错误。

如何将jQuery插件与React集成?

3 个答案:

答案 0 :(得分:11)

首先air-datepicker 不是React组件,它是jQuery插件,所以它不会像你的例子一样工作。

其次,为了正常工作,jQuery应该在全局环境中可用,最常用的方法是通过<script>标记将其包含在页面中。它应该包含之前插件脚本。

要检查它是否真正包含在全球范围内,只需输入Chrome控制台window.jQuery并按输入即可返回function (a,b){...}之类的内容。如果没有,当您做错事时,请检查src标记的<script>属性,也许它指向错误的目的地

如何在React中使用它?

嗯,最简单的方法就是在组件中的任何DOM元素上初始化插件,就像在常规JavaScript中一样。

&#13;
&#13;
class MyComponent extends React.Component {
  componentDidMount(){
    this.initDatepicker();
  }
  
  initDatepicker(){
    $(this.refs.datepicker).datepicker();
  }
  
  render(){
    return (
      <div>
        <h3>Choose date!</h3>
        <input type='text'  ref='datepicker' />
      </div>    
    )
  }
}

ReactDOM.render(
  <MyComponent />,
  document.getElementById('app')
);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.min.css" rel="stylesheet"/>

<div id="app"></div>
&#13;
&#13;
&#13;

作为单独的组件

非常简单的datepicker React组件示例

&#13;
&#13;
class Datepicker extends React.Component {
  componentDidMount(){
    this.initDatepicker();
  }
  
  initDatepicker(){
    $(this.refs.datepicker).datepicker(this.props);
  }

  render(){
    return (
      <input type='text' ref='datepicker'/>
    )
  }
}


class MyComponent extends React.Component {
  render(){
    return (
      <div>
        <h3>Choose date from React Datepicker!</h3>
        <Datepicker range={true} />
      </div>    
    )
  }
}

ReactDOM.render(
  <MyComponent />,
  document.getElementById('app')
);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.min.css" rel="stylesheet"/>

<div id="app"></div>
&#13;
&#13;
&#13;

另外,请不要忘记包含 css 文件。

答案 1 :(得分:3)

air-datepicker不是React组件。它是jquery-plugin,因此我们可以按照本教程https://reactjs.org/docs/integrating-with-other-libraries.html

import React from 'react'
import $ from 'jquery'
import 'air-datepicker/dist/js/datepicker.js'
import 'air-datepicker/dist/css/datepicker.css'

class AirDatepicker extends React.Component {
    componentDidMount(){
        this.$el = $(this.el)
        this.$el.datepicker()
    }

    render() {
        return (
            <div>
                <input ref={el => this.el = el}/>
            </div>
        )
    }
}

export default AirDatepicker

但是它仍然不起作用,因为jquery而不是air-datepicker的依赖项。

ReferenceError: jQuery is not defined
./node_modules/air-datepicker/dist/js/datepicker.js
D:/demo/react/jq-plugin/datepicker/node_modules/air-datepicker/dist/js/datepicker.js:2236
  2233 |         }
  2234 |     };
  2235 | })();
> 2236 |  })(window, jQuery);
  2237 | 
  2238 | 
  2239 | //////////////////

按照错误消息进行操作,最简单的方法是加密air-datepicker。在jQuery

的第一行中将air-datepicker/dist/js/datepicker.js添加为依赖项

之前:

  1 |;(function (window, $, undefined) { ;(function () {
  2 |    var VERSION = '2.2.3',
  3 |        pluginName = 'datepicker'

之后:

> 1 |import jQuery from 'jquery'
  2 |;(function (window, $, undefined) { ;(function () {
  3 |    var VERSION = '2.2.3',
  4 |        pluginName = 'datepicker'

因此<AirDatepicker />可以很好地用作React组件。

答案 2 :(得分:1)

我的解决方案是

import 'stupid-table-plugin';
...


async componentDidMount() {
   window.$("table").stupidtable()
}