reactjs - jsx'i tsx'e doğru bir şekilde nasıl dönüştürürsünüz


Mevcut jsx dosyasını npx gltfjsx komutundan TypeScript dosyasına doğru şekilde nasıl dönüştürebilirim? Hataları düzeltemedim.

import React, {useRef} from 'react'
import { useGLTF } from '@react-three/drei'

export default function Crown(state: any, { ...props }) {
    const group = useRef()
    // @ts-ignore
    const { nodes, materials } = useGLTF('/crown.glb') //TS2339: Property 'materials' does not exist on type 'GLTF'.
    return (
        <group
            // @ts-ignore
            ref={group} //TS2322: Type 'MutableRefObject<undefined>' is not assignable to type 'Ref<Group> | undefined'.   Type 'MutableRefObject<undefined>' is not assignable to type 'RefObject<Group>'.     Types of property 'current' are incompatible.       Type 'undefined' is not assignable to type 'Group | null'.
            {...props}
            dispose={null}
        >
            <mesh..../>
            <mesh..../>
            <mesh..../>
            <mesh..../>
        </group>
    )
}

useGLTF.preload('/crown.glb')

Cevap #1

gltfjsx modülünün kullanılabilir bir types bayrağı vardır, böylece bunu manuel olarak yapmanıza gerek yoktur.

Bir .glb dosyasını .tsx dosyasına dönüştürmek istediğimde, basitçe npx gltfjsx file.glb -t veya npx gltfjsx file.glb --types yapıyorum

Bazen bu, ref için bir hata verir; eğer ref'e ihtiyacınız yoksa silebilirsiniz, eğer ihtiyacınız varsa benim çözümüm ref türünü tanımlamaktı:

 <group
      ref={group as Ref<THREE.Group>}
      {...props}
      dispose={null}
    >
...
</group>

İlgili makaleler