gpt4 book ai didi

javascript - 使用 eventRender 在完整日历 api 中附加子元素?

转载 作者:行者123 更新时间:2023-11-30 23:59:44 25 4
gpt4 key购买 nike

我的应用程序中有一个简单的 SVG,现在悬停时,我想将此 div 动态附加到完整日历 API 中的某个元素

UPDATE: Here is my full component

import React, { useState, useEffect, useContext, useRef } from 'react';
import { Calendar as FullCalendar } from '@fullcalendar/core';
import googleCalendarPlugin from '@fullcalendar/google-calendar';
import dayGridPlugin from '@fullcalendar/daygrid';
import timeGridPlugin from '@fullcalendar/timegrid';
import interactionPlugin from '@fullcalendar/interaction';
import calendartopbg from 'assets/images/calendar_bg.png';
import cloud from 'assets/images/calendar_chmurka.png';
import styled from 'styled-components';

const Calendar = () => {
const [calendar, setCalendar] = useState('');
const calendarRef = useRef('');

useEffect(() => {
if (calendarRef.current) {
console.log(FullCalendar);
setCalendar(new FullCalendar(calendarRef.current, {
plugins: [dayGridPlugin, timeGridPlugin, interactionPlugin, googleCalendarPlugin],
theme: false,
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,listYear',

},
views: {
day: {
titleFormat: 'dddd, MMMM Do YYYY'
}
},

googleCalendarApiKey: 'AIzaSyDcnW6WejpTOCffshGDDb4neIrXVUA1EAE',
events: 'en.usa#holiday@group.v.calendar.google.com',
eventClick: function(arg) {

// opens events in a popup window
window.open(arg.event.url, '_blank', 'width=700,height=600');

// prevents current tab from navigating
arg.jsEvent.preventDefault();
},
eventRender: function(info){
console.log(info.el);
var imageData = document.getElementById('imageDataContaainer')

console.log(imageData)

info.el.appendChild(imageData);
},
}));
}
}, [calendarRef.current]);

console.log(calendar);

return (
<>
<CalendarBackgroundWrapper>
<img style={imagestyle} src={calendartopbg} />
</CalendarBackgroundWrapper>

<CalendarContainer ref={calendarRef}>
{calendar && calendar.render ? calendar.render(): null}
</CalendarContainer>
<div id="imageDataContaainer">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<defs>
<mask id="image-mask">
<circle id="outer" cx="50" cy="50" r="50" fill="white"/>
<circle id="inner" cx="50" cy="50" r="25"/>
</mask>
</defs>
</svg>
</div>

</>
)
}

export default Calendar;

在控制台上我可以看到这个 div ,然后出现以下错误

app.js?1:121281 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
at Calendar.eventRender (app.js?1:121281)

我的代码有什么问题吗?

最佳答案

我不确定你想做什么以及为什么你需要一个“事件渲染”,但是 "React way" is conditional rendering一个组件。

改变 DOM(如追加子项)是一种反模式,as it's React's responsibility.

const App = () => {
const [isRender, toggle] = useReducer(p => !p, false);

return (
<>
{isRender && <SVGComponent />}
<button onClick={toggle}>Render SVG</button>
</>
);
};

Edit great-noether-kn5mp

关于javascript - 使用 eventRender 在完整日历 api 中附加子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60813398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com