Zyrus Posted September 11 Posted September 11 Hi! I am having a hard time figuring out how to make the quickSetterreusable. what I meant by this is I want to use it inside and outside useGSAP() I am scratching my head how can I make this work, even asked AI for assistance but no luck. here is my scenario and why I want it to be resuable export default function MouseDemo() { useGsap(( //workaround: create quickSetter-A //I want to use the quickSetter here )=>{},{scope: container}) function handleMouseMove(){ //I want to use the quickSetter here //workaround: create a similar quickSetter-A } } and here is what I want to achieve: export default function MouseDemo() { //create a const setScaleX = useRef<gsapQuickSetter>(null) useGsap(()=>{ //set up quickSetter //setScaleX = (target , {scaleX}); },{scope: container}) function handleMouseMove(){ //invoke setScaleX(value); } } I can't figure out the exact return type of quickSetter. hovering from the existing quickSetter reveals this paramter and return type function gsap.quickSetter(targets: gsap.TweenTarget, property: string, unit?: string): Function that it confirms that it returns a function. my question is what kind of function? what its parameters and return type. I even tried to dig in gsap.core but couldn't find any leads. here is my current attempt I created a type alias that accept this kind of pattern type QuickSetterFn = (value: number | string) => void; then I referenced it to a useRef but still not luck type Coordinates = { x: number; y: number }; type QuickSetterFn = (value: number | string) => void; export default function MouseDemo() { //useRef const container = useRef<HTMLDivElement | null>(null); const setScaleX = useRef<QuickSetterFn | null>(null); //state const [coordinates, setCoordinates] = useState<Coordinates>({ x: 0, y: 0 }); //useGsap - contextSafe const { contextSafe } = useGSAP( () => { //quickSetter setup setScaleX.current = gsap.quickSetter(".box", "scaleX"); here is what I get Type 'Function' is not assignable to type 'QuickSetterFn'. Type 'Function' provides no match for the signature '(value: string | number): void'. right now I use the workaround , to create a duplicate quickSetter for the button the the useGsap. --- some attempts: const setScaleX = useRef(null); setScaleX.current = gsap.quickSetter(".box", "scaleX"); error: Type 'Function' is not assignable to type 'null'. -------------------------------- const setScaleX = useRef<Function>(null); The `Function` type accepts any function-like value. Prefer explicitly defining any function parameters and return type.
Solution GreenSock Posted September 11 Solution Posted September 11 Hi Zyrus, Welcome to the forums! You’re very close — this is mostly a TypeScript typing issue, not a quickSetter limitation. gsap.quickSetter() returns a plain function that you call with the next value: let setScaleX = gsap.quickSetter(".box", "scaleX"); setScaleX(1.5); // set scaleX immediately If you pass a unit as the 3rd argument, feed it a number and GSAP appends the unit for you: let setX = gsap.quickSetter(".box", "x", "px"); setX(100); // → x: 100px In the official GSAP 3 types that return type is the broad built-in Function, not a more specific callback type: function quickSetter(...): Function; So when you do: const setScaleX = useRef<QuickSetterFn | null>(null); setScaleX.current = gsap.quickSetter(".box", "scaleX"); TypeScript correctly complains that Function isn’t assignable to (value: number | string) => void. Your QuickSetterFn alias is fine — you just need to assert on assignment. Pattern that works: import { useRef } from "react"; import gsap from "gsap"; import { useGSAP } from "@gsap/react"; type QuickSetterFn = (value: number | string) => void; export default function MouseDemo() { const container = useRef<HTMLDivElement | null>(null); const setScaleX = useRef<QuickSetterFn | null>(null); useGSAP(() => { // Create once when the DOM / scope is ready setScaleX.current = gsap.quickSetter(".box", "scaleX") as QuickSetterFn; }, { scope: container }); const handleMouseMove = (e: React.MouseEvent) => { // Reuse the same setter outside useGSAP setScaleX.current?.(/* your value */); }; return ( <div ref={container} onMouseMove={handleMouseMove}> <div className="box" /> </div> ); } A couple of notes: You only need one quickSetter. Create it inside useGSAP (so the scoped .box exists), stash it on a ref, then call that ref from your mouse handler. No need to build a second copy in handleMouseMove. Prefer an element ref when you can. Selectors are fine with { scope: container }, but targeting the node directly is a bit clearer: const box = useRef<HTMLDivElement | null>(null); useGSAP(() => { if (!box.current) return; setScaleX.current = gsap.quickSetter(box.current, "scaleX") as QuickSetterFn; }, { scope: container }); quickSetter is just a setter function — it doesn’t create a tween that needs cleanup the way gsap.to() does. Storing it on a ref and calling it from an event handler is totally normal. Use contextSafe() when you’re creating animations inside event handlers that should be cleaned up with the context; you don’t need it just to call an already-created quickSetter. If you’re driving this from pointer position, call the setter directly from the event (or a quickTo if you want smoothing). Putting mouse coords into React state on every mousemove will re-render a lot and fight the performance win that quickSetter is meant for. Happy tweening!
Zyrus Posted September 12 Author Posted September 12 Thank you! been scratching my head and switching my codes here and there.. I only need that 'as' keyword in order to work. now my QuickSetFn is now reusable! thank you! useGSAP( () => { if (!setScaleX.current) return; setScaleX.current = gsap.quickSetter( container.current, "scaleX", ) as QuickSetterFn; if (!setScaleY.current) return; setScaleY.current = gsap.quickSetter( container.current, "scaleX", ) as QuickSetterFn; }, { scope: container }, ); then I invoke it inside my button function if (!setScaleX.current || !setScaleY.current) return; setScaleX.current(convertedScaleX); setScaleY.current(convertedScaleX); I use bothScaleX because I am at the part 2 of the exercise I am trying to learn. now I can continue where I left at. thank you! 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now