Coder Social home page Coder Social logo

[feature] useHelper about drei HOT 7 CLOSED

pmndrs avatar pmndrs commented on July 30, 2024 1
[feature] useHelper

from drei.

Comments (7)

drcmda avatar drcmda commented on July 30, 2024 1

let's add it!

from drei.

drcmda avatar drcmda commented on July 30, 2024 1

users should be able to do:

useHelper<SpotLightHelper>(ref, SpotLightHelper, color)
function useHelper<T>(
  ref: React.MutableRefObject<Object3D>,
  proto: T,
  ...args: ConstructorParameters<T>
) {

from drei.

drcmda avatar drcmda commented on July 30, 2024 1

that's fine, thats how i do it as well most of the time, eventually it will annoy someone and they fix it ;-)

from drei.

gsimone avatar gsimone commented on July 30, 2024

Alright, I've made some progress, I'll share here before a pr as I'm not sure about some details.

https://codesandbox.io/s/r3f-use-helper-bm2qu?file=/src/use-helper.tsx

  • I'm not sure how to type the arguments to the hook, the second one should be an helper constructor but I can't seem to find any type in threejs that could work for all helpers
  • the rest of the arguments would be passed to the constructor, don't know if is the correct way to do it

from drei.

gsimone avatar gsimone commented on July 30, 2024

So, I updated the sandbox with your suggestions, still:

useHelper<BoxHelper>(mesh, BoxHelper, "cyan");

gives me a type error "Argument of type 'typeof BoxHelper' is not assignable to parameter of type 'BoxHelper'."

This is fixed using useHelper<typeof BoxHelper>(...) ( and I'm not sure if this is desirable)

in the hook I get these type errors:
Type 'T' does not satisfy the constraint 'new (...args: any) => any'
and
This expression is not constructable. when I try to create the new helper instance.

I'm not sure if there is a simpler approach or I'm missing some piece of ts knowledge to make this work

from drei.

drcmda avatar drcmda commented on July 30, 2024

i dont know TS myself, but in reactthreefiber they have solved args in /src/three-types.ts maybe you find something over there

from drei.

gsimone avatar gsimone commented on July 30, 2024

I fixed the warning but I think that made everything too "any"ish, maybe I'll just PR now and open an issue later to have somebody more expert with TS check the typings

from drei.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.