React和设计良好的jQuery插件并没有什么不兼容的问题。
生活随笔
收集整理的這篇文章主要介紹了
React和设计良好的jQuery插件并没有什么不兼容的问题。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
著作權(quán)歸作者所有。
商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
作者:fluxxu
鏈接:http://www.zhihu.com/question/36045843/answer/67064767
來源:知乎
React和設(shè)計良好的jQuery插件并沒有什么不兼容的問題。
我所說的設(shè)計良好的插件,就是你給他一個Element,他只在這個Element里面操作,不到處瞎搞。還有就是它提供了安全destroy的方法可以調(diào)用。
一般是這樣,比如你想用某個jQuery的autocomplete插件,在jQuery里面你這樣初始化:
$('#input').autocomplete(); 在React里面,你給包裝一下變成這樣:
React.createClass({shouldComponentUpdate() { return false; //告訴react這個component我們打算自己瞎搞,叫它別碰 }, componentDidMount() { $(this.getDOMNode()).autocomplete(); }, componentWillUnmount() { $(this.getDOMNode()).autocomplete('destroy'); //調(diào)用插件的清理函數(shù) }, render() { return <input type="text" />; } }); 偶爾有腦殘插件,非要拿一個selector字符串初始化的,你可以這樣對付:
var counter = 0;React.createClass({shouldComponentUpdate() {return false;},componentWillMount() {this.__elementID = 'naocan_' + (++counter); //強(qiáng)制給他分配一個唯一的id},componentDidMount() {this.__instance = naocan.init({selector: '#'+this.__elementID});},componentWillUnmount() {this.__instance.destroy();},render() {return <input id={this.__elementID} type="text" />;} });
商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
作者:fluxxu
鏈接:http://www.zhihu.com/question/36045843/answer/67064767
來源:知乎
React和設(shè)計良好的jQuery插件并沒有什么不兼容的問題。
我所說的設(shè)計良好的插件,就是你給他一個Element,他只在這個Element里面操作,不到處瞎搞。還有就是它提供了安全destroy的方法可以調(diào)用。
一般是這樣,比如你想用某個jQuery的autocomplete插件,在jQuery里面你這樣初始化:
$('#input').autocomplete(); 在React里面,你給包裝一下變成這樣:
React.createClass({shouldComponentUpdate() { return false; //告訴react這個component我們打算自己瞎搞,叫它別碰 }, componentDidMount() { $(this.getDOMNode()).autocomplete(); }, componentWillUnmount() { $(this.getDOMNode()).autocomplete('destroy'); //調(diào)用插件的清理函數(shù) }, render() { return <input type="text" />; } }); 偶爾有腦殘插件,非要拿一個selector字符串初始化的,你可以這樣對付:
var counter = 0;React.createClass({shouldComponentUpdate() {return false;},componentWillMount() {this.__elementID = 'naocan_' + (++counter); //強(qiáng)制給他分配一個唯一的id},componentDidMount() {this.__instance = naocan.init({selector: '#'+this.__elementID});},componentWillUnmount() {this.__instance.destroy();},render() {return <input id={this.__elementID} type="text" />;} });
轉(zhuǎn)載于:https://www.cnblogs.com/HuiLove/p/4923059.html
總結(jié)
以上是生活随笔為你收集整理的React和设计良好的jQuery插件并没有什么不兼容的问题。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios 地图
- 下一篇: Android的BUG(四) - And