ReferenceError:在NextJS项目中未定义CustomEvent

时间:2020-08-17 12:45:40

标签: reactjs next.js

我遇到错误: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
}

1 个答案:

答案 0 :(得分:0)

将其放在useEffect钩中,它将起作用。