Importing JQuery

To install JQuery:

Installing jQuery via npm
npm install jquery

In general, you should try to access the DOM nodes only after the initial render – which is exactly what the useEffect Hook with an empty dependency array below achieves.


import $ from 'jquery';import {useState, useEffect} from 'react';function App() {
  const [n, setN] = useState(0);
  useEffect(()=>{
    $("button").on('click', ()=>{
      setN(n=>(n+1));
    })
  },[])
  return (
    <button>{n}</button>
  );}export default App;