├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # useJquery - An essential hook for your React applications 2 | 3 | You know how they say the only thing Internet Explorer is good for is downloading other browsers? In a similar manner the only reasonable way to use React for building real-world applications is by combining it with jQuery, powerful framework for Web artisans. 4 | 5 | ## Install 6 | ``` 7 | $ yarn add react-use-jquery 8 | or 9 | $ npm install --save react-use-jquery 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```jsx harmony 15 | import React, { useEffect } from 'react'; 16 | import useJquery from 'react-use-jquery'; 17 | 18 | export default function App() { 19 | const $ = useJquery(); 20 | 21 | useEffect(() => { 22 | if ($) { 23 | $('.App').html('Hello world'); 24 | } 25 | }, [$]); 26 | 27 | return
; 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import { useState } from "react"; 2 | 3 | export default function usejQuery(version = '3.3.1') { 4 | const [jQuery, setjQuery] = useState(window.jQuery); 5 | 6 | if (!jQuery) { 7 | const script = document.createElement('script'); 8 | script.type = 'text/javascript'; 9 | script.async = true; 10 | script.onload = function() { 11 | setjQuery(window.jQuery); 12 | }; 13 | script.src = `//code.jquery.com/jquery-${version}.min.js`; 14 | document.getElementsByTagName('head').item(0).appendChild(script); 15 | } 16 | 17 | return window.jQuery; 18 | } 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-use-jquery", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "peerDependencies": { 10 | "react": "^16.8.x", 11 | "jquery": "^3.3.1" 12 | }, 13 | "author": "", 14 | "license": "ISC" 15 | } 16 | --------------------------------------------------------------------------------