网站的这个组件在.jsx中,而整个网站在.tsx中.我想将此文件转换为.tsx文件.有没有人能帮忙或 bootstrap .无论我做什么,我都会遇到越来越多的错误.

extend({ RoundedPlaneGeometry: geometry.RoundedPlaneGeometry })

function Scene({ children, ...props }) {
  const ref = useRef()
  const scroll = useScroll()
  const [hovered, hover] = useState(null)
  useFrame((state, delta) => {
    ref.current.rotation.y = -scroll.offset * (Math.PI * 2) // Rotate contents
    state.events.update() // Raycasts every frame rather than on pointer-move
    easing.damp3(state.camera.position, [-state.pointer.x * 2, state.pointer.y * 2 + 4.5, 9], 0.3, delta)
    state.camera.lookAt(0, 0, 0)
  })
  return (
    <group ref={ref} {...props}>
      <Cards category="spring" from={0} len={Math.PI / 4} onPointerOver={hover} onPointerOut={hover} />
      // More Cards
    </group>
  )
}

function Cards({ category, data, from = 0, len = Math.PI * 2, radius = 5.25, onPointerOver, onPointerOut, ...props }) {
  const [hovered, hover] = useState(null)
  const amount = Math.round(len * 22)
  const textPosition = from + (amount / 2 / amount) * len
  return (
    <group {...props}>
      <Billboard position={[Math.sin(textPosition) * radius * 1.4, 0.5, Math.cos(textPosition) * radius * 1.4]}>
        <Text font={suspend(inter).default} fontSize={0.25} anchorX="center" color="black">
          {category}
        </Text>
      </Billboard>
      {Array.from({ length: amount - 3 /* minus 3 images at the end, creates a gap */ }, (_, i) => {
        const angle = from + (i / amount) * len
        return (
          <Card
            key={angle}
            onPointerOver={(e) => (e.stopPropagation(), hover(i), onPointerOver(i))}
            onPointerOut={() => (hover(null), onPointerOut(null))}
            position={[Math.sin(angle) * radius, 0, Math.cos(angle) * radius]}
            rotation={[0, Math.PI / 2 + angle, 0]}
            active={hovered !== null}
            hovered={hovered === i}
            url={`/img${Math.floor(i % 10) + 1}.jpg`}
          />
        )
      })}
    </group>
  )
}

function Card({ url, active, hovered, ...props }) {
  const ref = useRef()
  useFrame((state, delta) => {
    const f = hovered ? 1.4 : active ? 1.25 : 1
    easing.damp3(ref.current.position, [0, hovered ? 0.25 : 0, 0], 0.1, delta)
    easing.damp3(ref.current.scale, [1.618 * f, 1 * f, 1], 0.15, delta)
  })
  return (
    <group {...props}>
      <Image ref={ref} url={url} scale={[1.618, 1, 1]} side={THREE.DoubleSide} />
    </group>
  )
}

function ActiveCard({ hovered, ...props }) {
  const ref = useRef()
  const name = useMemo(() => generate({ exactly: 2 }).join(' '), [hovered])
  useLayoutEffect(() => void (ref.current.material.zoom = 0.8), [hovered])
  useFrame((state, delta) => {
    easing.damp(ref.current.material, 'zoom', 1, 0.5, delta)
    easing.damp(ref.current.material, 'opacity', hovered !== null, 0.3, delta)
  })
  return (
    <Billboard {...props}>
      <Text font={suspend(inter).default} fontSize={0.5} position={[2.15, 3.85, 0]} anchorX="left" color="black">
        {hovered !== null && `${name}\n${hovered}`}
      </Text>
      <Image ref={ref} transparent position={[0, 1.5, 0]} url={`/img${Math.floor(hovered % 10) + 1}.jpg`}>
        <roundedPlaneGeometry parameters={{ width: 3.5, height: 1.618 * 3.5 }} args={[3.5, 1.618 * 3.5, 0.2]} />
      </Image>
    </Billboard>
  )
}

我用了ChatGPT,但更糟了!例如,我甚至不知道这个错误是什么意思"类型‘MuableRefObject&lt;Group|Unfined&>’不可分配给类型‘Ref&lt;Group&>t;|Unfined’." 类型‘MuableRefObject&lt;Group|Unfined&gt;’不可分配给类型‘RefObject&lt;Group&gt;’. 属性"Current"的类型不兼容. 类型‘Group|UnDefined’不可分配给类型‘Group|Null’. 类型‘UNDEFINED’不可分配给类型‘Group|NULL’.ts(2322)"!

推荐答案

您收到的错误与TypeScrip强制执行的类型安全有关.

粘贴的错误与try 分配"MutableRefObject Group"类型的值有关<|undefined"转换>为只接受类型"Ref"的对象|未定义'.

主要区别在于:

  • Ref:React.createRef()创建的不可变的ref对象
  • MuableRefObject:由useRef()创建,是可变的

如果你试图确保/集成你提供的代码作为一个Typescript文件,你需要确保变量类型/用法是一致的.(这是Typescript相对于JavaScript存在的主要原因之一(它允许不太严格的类型,因此可能导致意外的错误).

在修复这些错误方面,您需要判断您提供的代码并修改类型声明,使其不使用useRef(),因为这使它们成为MutableRefObject‘S,而不是所需的Ref类型.如果您想要它的另一种方式,那么您将需要更改正在try 只接受类型Ref的输入的任何东西,以便它接受您提供的MutableRefObject个值.

希望这能帮助你朝着正确的方向前进!

Javascript相关问答推荐

容器如何更改默认插槽中子项的显示?

React Native平面列表自动滚动

防止用户在selectizeInput中取消 Select 选项

类型自定义lazy Promise. all

react—router v6:路由没有路径

如何使覆盖div与可水平滚动的父div相关?

配置WebAssembly/Emscripten本地生成问题

无法访问Vue 3深度监视器中对象数组的特定对象值'

如何使用子字符串在数组中搜索重复项

使用ThreeJ渲染的形状具有抖动/模糊的边缘

JQuery Click事件不适用于动态创建的按钮

基于props 类型的不同props ,根据来自接口的值扩展类型

为什么我的自定义元素没有被垃圾回收?

处理app.param()中的多个参数

MongoDB中的嵌套搜索

为什么我看到的是回复,而不是我的文档?

如果对象中的字段等于某个值,则从数组列表中删除对象

在Java脚本中构建接口的对象

为什么这个最小Angular 的Licial.dev设置不起作用?

JSX/React -如何在组件props 中循环遍历数组