I’m trying to open a modal window using bootstrap 5 (not the library React-Bootstrap) when cliking a Day in the FullCalendar Component in React.
I tried accesing through the modal using $ of JQuery but I get this error message "$ is not a function". I try importing the JQuery library before the bootstrap one, adding the "import $ from Jquery" line and nothing works.
I also have this implementation but it doesn’t work so I don’t know what I’m missing.
const CalendarContainerPage = () => {const modalRef = useRef(null);
const [showModal, setShowModal] = useState(false);
const select = (info) => {
alert('selected ' + info.start + ' to ' + info.end);
}
console.log(showModal)
const handleDayClick = () => {
setShowModal(true);
};
const handleCloseModal = () => {
setShowModal(false);
};
useEffect(() => {
if(showModal){
modalRef..toggle()
}
},[showModal])
const randomColor= "#"+((1<<24)*Math.random()|0).toString(16) + "";
const eventObject = [
{ // this object will be "parsed" into an Event Object
groupId: 'blueEvents',
title: 'Congress', // a property!
start: '2023-06-21',
end:'2023-06-22',
startRecur: '2023-06-18T09:00:00',
endRecur: '2023-06-29T18:00:00',
startTime: '12:30:00', // a property!
endTime: '13:30:00', // a property! ** see important note below about 'end' **
daysOfWeek: [ '1','2' ],
display: 'block',
color : randomColor,
}
]
console.log(eventObject);
const eventClick = (info) => {
alert('Event: ' + info.event.title)
}
return(
<div className='calendarBoard'>
<FullCalendar
plugins={[ dayGridPlugin, interactionPlugin ]}
locale = {esLocale}
initialView="dayGridMonth"
height='100%'
selectable={true}
select = { handleDayClick}
events = {eventObject}
eventClick={eventClick}
handleWindowResize={true}
/>
{showModal && (<div className="modal " ref={modalRef} id="ModalDay" tabindex="-1" aria-labelledby="ModalDay" aria-hidden="true" >
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="exampleModalLabel"> "text"</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body px-4">
Modal
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal" onClick={handleCloseModal}>Cerrar</button>
<button type="button" className="btn btn-primary">Guardar Cambios</button>
</div>
</div>
</div>
</div>)}
</div>
)
}
export default CalendarContainerPage
I also tried using useRef but I’m not sure if I’m using it correctly. (I’m new with React)
2
Answers
Just in case someone else has the same issue, I post here the solution I found:
Basically I pick the Modal window using the boostrap.Modal function inside a useEffect to be sure to work with the modal after it is completly loaded:
And call the click Handler of the day:
I found this solution using the bootstrap 5 documentation: [https://getbootstrap.com/docs/5.0/components/modal/#methods]
Have you imported the necessary dependencies ?
Let’s check the following requirements:
Install Bootstrap and its dependencies:
Import the necessary components and dependencies in your file:
Import the Bootstrap CSS in your project’s main file (index.js or App.js):
Modified code: