Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法转动转盘 #191

Closed
pointline opened this issue Dec 17, 2021 · 1 comment
Closed

无法转动转盘 #191

pointline opened this issue Dec 17, 2021 · 1 comment

Comments

@pointline
Copy link
Contributor

pointline commented Dec 17, 2021

  • 你当前是什么框架(必填):
    Next.js React Typescript
  • 你使用的是哪个包(必填):
    lucky-canvas
  • 你当前插件的版本(必填):
    @1.7.13
  • 当前环境是小程序还是浏览器(选填):
    浏览器
  • 详细描述你的bug:
    当我参照楼主写的React class组件,封装一个函数组件时,转盘无法转动。
    image
  • 问题代码(重要):
// 代码开始, 别再放歪了行吗
import {
  FC,
  useRef,
  useEffect,
  useCallback,
  forwardRef,
  useImperativeHandle
} from 'react'
import { LuckyWheel as Wheel } from 'lucky-canvas'

import styled from './luckyWheel.module.less'

export type LuckyWheelRef = {
  play: () => void
  stop: (index: number) => void
}

const LuckyWheel: FC<any> = forwardRef((props, ref) => {
  const luckyWheelEl = useRef<HTMLDivElement>(null)
  const luckyWheelRef = useRef<any>()

  const init = useCallback(() => {
    if (!luckyWheelEl.current) return

    luckyWheelRef.current = new Wheel(
      {
        flag: 'WEB',
        divElement: luckyWheelEl.current,
        width: props.width,
        height: props.width
      },
      {
        ...props,
        start: (...rest) => {
          props.onStart && props.onStart(...rest)
        },
        end: (...rest) => {
          props.onEnd && props.onEnd(...rest)
        }
      }
    )

    console.log(luckyWheelRef.current)
  }, [props])

  const play = () => {
    luckyWheelRef.current && luckyWheelRef.current.play()
  }

  const stop = (index: number) => {
    luckyWheelRef.current && luckyWheelRef.current.stop(index)
  }

  useEffect(() => {
    try {
      init()
    } catch (e) {
      //
    }
  }, [init])

  useImperativeHandle(
    ref,
    () => ({
      play,
      stop,
      luckyWheelRef
    }),
    []
  )

  return <div className={styled.wheel} ref={luckyWheelEl}></div>
})

LuckyWheel.displayName = 'LuckyWheelRef'

export default LuckyWheel
// 代码结束

感谢!!!

@pointline pointline changed the title 无法转动抓盘 无法转动转盘 Dec 17, 2021
@pointline pointline reopened this Dec 17, 2021
@buuing
Copy link
Owner

buuing commented Dec 17, 2021

.....我不会react, 所以我也不知道为啥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants