jquery react code example
Example 1: jquery in react
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
$("button").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
Example 2: jquery in react
Yes, we can use jQuery in ReactJs. Here I will tell how we can use it using npm.
step 1: Go to your project folder where the package.json file is present via using terminal using cd command.
step 2: Write the following command to install jquery using npm : npm install jquery --save
step 3: Now, import $ from jquery into your jsx file where you need to use.
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
$("button").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
Example 3: use javascript library in react
class MainView extends Component {
render() {
return <div/>;
},
componentDidMount() {
const node = ReactDOM.findDOMNode(this);
$(node).activateMyCoolWidget();
ReactDOM.render(<div>{this.props.children}</div>, node);
}
});
Example 4: react in jquery
class SomePlugin extends React.Component {
componentDidMount() {
this.$el = $(this.el); this.$el.somePlugin(); }
componentWillUnmount() {
this.$el.somePlugin('destroy'); }
render() {
return <div ref={el => this.el = el} />; }
}