Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'type error.... $(...).modal is not a function

I am trying to create and edit function to update notes by user (in a REACT project), using bootstrap modals, but here is the error I get

Uncaught (in promise) TypeError: jquery__WEBPACK_IMPORTED_MODULE_1___default()(...).modal is not a function
updateNote Notes.js:16
onClick NoteItem.js:15
React 23
js index.js:6
factory react refresh:6
Webpack 3
    __webpack_require__
    <anonymous>
    <anonymous>

I tried going through some posts on the forum like including jquery library before bootstrap, but it didn't work.

Here is my code:

    import React,{ useContext,useEffect,useRef } from "react";
import $ from 'jquery'
import 'bootstrap';
import noteContext from "../context/notes/NoteContext"
import AddNote from "./AddNote";
import NoteItem from "./NoteItem";

export const Notes=() => {
    const context=useContext(noteContext)
    const { notes,getNotes }=context
    useEffect(() => {
        getNotes()
        // eslint-disable-next-line
    },[])
    const ref=useRef(null)
    const updateNote=async (note) => {
        console.log("working")
        $(ref).modal('toggle')
    }
    return (
        <>
            <AddNote />
            <button ref={ref} type="button" className="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
                Launch demo modal
            </button>

            <div className="modal fade" id="exampleModal" tabIndex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div className="modal-dialog" role="document">
                    <div className="modal-content">
                        <div className="modal-header">
                            <h5 className="modal-title" id="exampleModalLabel">Edit Note</h5>
                            <button type="button" className="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div className="modal-body">
                            ...
                        </div>
                        <div className="modal-footer">
                            <button type="button" className="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" className="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>
            <div className="row my-3">
                <h1>Your Notes</h1>
                {notes.map((note) => {
                    return <NoteItem key={note._id} updateNote={updateNote} note={note} />
                })}
            </div>
        </>
    )
}

Also here is my index.html (I removed the jquery link as it wasn't working):

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="theme-color" content="#000000" />
  <meta name="description" content="Web site created using create-react-app" />
  <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
  <title>iNotebook - Your notes on the cloud</title>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
</head>

<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root">
    <script src="https://kit.fontawesome.com/ae578fcfec.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa"
      crossorigin="anonymous"></script>
  </div>
</body>

</html>

What am I missing?

I found another post which had a similar issue (React Error : __WEBPACK_IMPORTED_MODULE_4_jquery___default(...)(...).modal is not a function) importing bootstrap didn't work either because It had dependency issue with @popperjs/core which i installed but then got more errors:

Module not found: Error: Can't resolve './modifiers/index.js' in '/home/alien/codes/webDev/react/inotebook/node_modules/@popperjs/core/lib'
[0] ERROR in ./node_modules/@popperjs/core/lib/index.js 2:0-37
[0] Module not found: Error: Can't resolve './modifiers/index.js' in '/home/alien/codes/webDev/react/inotebook/node_modules/@popperjs/core/lib'
[0] 
[0] ERROR in ./node_modules/@popperjs/core/lib/popper.js 20:0-37
[0] Module not found: Error: Can't resolve './modifiers/index.js' in '/home/alien/codes/webDev/react/inotebook/node_modules/@popperjs/core/lib'
[0] 
[0] webpack compiled with 2 errors

then I didn't find anything to solve it, let me know if this method was relevant or not or if there is anything else i need to add to this

Just to give a better idea here is the tutorial i am following: https://youtu.be/J5hGX5_XZDk . it is quite old so there must be an update in syntax in newer versions, which i am not sure of, so let me know if there is something that needs to change. And before you ask, initially i followed this tutorial by every word, but then i had to go through official bootstrap documentation to remove some errors which made the app crash, so is there something else that i need to change too?

updated code:

import React,{ useContext,useEffect,useRef } from "react";
import noteContext from "../context/notes/NoteContext"
import AddNote from "./AddNote";
import NoteItem from "./NoteItem";

export const Notes=() => {
    const context=useContext(noteContext)
    const { notes,getNotes }=context
    useEffect(() => {
        getNotes()
        // eslint-disable-next-line
    },[])
    const ref=useRef(null)
    const updateNote=async (note) => {
        console.log("working")
        ref.current.click()
    }
    return (
        <>
            <AddNote />
            <button ref={ref} type="button" className="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
                Launch demo modal
            </button>

            <div className="modal fade" id="exampleModal" tabIndex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div className="modal-dialog" role="document">
                    <div className="modal-content">
                        <div className="modal-header">
                            <h5 className="modal-title" id="exampleModalLabel">Edit Note</h5>
                            <button type="button" className="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div className="modal-body">
                            ...
                        </div>
                        <div className="modal-footer">
                            <button type="button" className="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" className="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>
            <div className="row my-3">
                <h1>Your Notes</h1>
                {notes.map((note) => {
                    return <NoteItem key={note._id} updateNote={updateNote} note={note} />
                })}
            </div>
        </>
    )
}
like image 322
kashish patel Avatar asked Aug 26 '26 17:08

kashish patel


1 Answers

Bootstrap V5 has many breaking changes. One of them is the removal of jquery.

This is how bootstrap 5 would toggle a modal using javascript.

var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
  keyboard: false
});

myModal.toggle();

You can also show/hide modal with the data attributes. But its different from whats done in Bootsrap 4. They have namespaced the attributes to data-bs-* (from data-*)

Look at the documentation here: https://getbootstrap.com/docs/5.0/components/modal/#live-demo

You code uses data-toggle="modal" data-target="#exampleModal" but the example shows data-bs-toggle="modal" data-bs-target="#exampleModal".

Also data-dismiss became data-bs-dismiss

like image 151
naveen Avatar answered Aug 28 '26 05:08

naveen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!