我遇到错误:ReferenceError: CustomEvent is not defined
。
我知道CustomEvent仅在浏览器(客户端)中可用,但是我正在尝试将此函数用作事件:
const mouseEnterHandler = () => {
eventDispatcher('cursor', {
label: 'test',
})
}
onMouseEnter={mouseEnterHandler()}
export const eventCreator = (name: string, params: Record<string, any>): CustomEvent => {
const event = new CustomEvent(name, {
detail: params
})
return event
}
export const eventDispatcher = (name: string, params: Record<string, any>): CustomEvent => {
const event: CustomEvent = eventCreator(name, params)
document.dispatchEvent(event)
return event
}
答案 0 :(得分:0)
将其放在useEffect
钩中,它将起作用。