5

I decided to introduce TypeScript in my React js application.

Having a problem with a component that uses react-spring for coordinate interpolation.

BEFORE TypeScript my component was like this:


function Card() {
    const [props, set] = useSpring(() => (
        {
            xy: [0, 0],
            config: {mass: 10, tension: 550, friction: 140}
        }));

    const calc = (x, y) => [x - window.innerWidth / 2, y - window.innerHeight / 2]

    return (
        <div onMouseMove={({clientX: x, clientY: y}) => set({xy: calc(x, y)})}>
            <animated.div className="card1" style={{transform: props.xy.interpolate((x,y) => `translate3d(${x / 10}px,${y / 10}px,0)`)}}/>
        </div>
    );
}

everything is working properly.

AFTER typeScript:


function Card() {
    const [props, set] = useSpring(() => (
        {
            xy: [0, 0],
            config: {mass: 10, tension: 550, friction: 140}
        }));

    const calc = (x: number, y: number) => [x - window.innerWidth / 2, y - window.innerHeight / 2]

    return (
        <div onMouseMove={({clientX: x, clientY: y}) => set({xy: calc(x, y)})}>
            <animated.div className="card1" style={{transform: props.xy.interpolate((xy) => `translate3d(${xy[0] / 10}px,${xy[1] / 10}px,0)`)}}/>
        </div>
    );
}

It does not work, I have no compilation errors, simply no translation occurs. The "style" is not injected into the element.

rene
  • 41,474
  • 78
  • 114
  • 152
Manu
  • 51
  • 1
  • 3

4 Answers4

2

A little old question but maybe someone could need a better solution. Try to use the interpolate function as Stas Krul mentioned. but you don't have to seperate the properties x and y.

const trans = (x: number, y: number) => `translate3d(${xy[0] / 10}px,${xy[1] / 10}px,0)`

const Card = () => {
const [props, set] = useSpring<{ xy: number[] }>(() => (
    {
        xy: [0, 0],
        config: {mass: 10, tension: 550, friction: 140}
    }));

const calc = (x: number, y: number) => [x - window.innerWidth / 2, y - window.innerHeight / 2]

return (
    <div onMouseMove={({clientX: x, clientY: y}) => set({xy: calc(x, y)})}>
        <animated.div className="card1"
            style={{transform: interpolate(props.xy, trans)}}
        />
    </div>
);

}

Max K
  • 106
  • 1
  • 6
2

For me none of the previous answers helped with the issue.

Exploring the library more in dept, I found at the declaration file (node_modules/@react-spring/core/index.d.ts) these lines:

/** Map the value of one or more dependencies */
declare const to: Interpolator;
/** @deprecated Use the `to` export instead */
declare const interpolate: Interpolator;

So essentially, instead of using interpolate, you should now use to, as followed:

import { to } from "react-spring"

<animated.div style={{ transform: to(props.xy, trans) }} />
ale917k
  • 1,494
  • 7
  • 18
  • 37
1

I fixed the same problem by changing xy to separate left and top properties and then using interpolate function exported from react-spring package like here. Still not a perfect solution but it's certainly better than suppressing errors using // @ts-ignore.

Stas Krul
  • 11
  • 2
0

I had a similar problem. For hotfix just put // @ts-ignore:

function Card() {
    const [props, set] = useSpring(() => (
        {
            xy: [0, 0],
            config: {mass: 10, tension: 550, friction: 140}
        }));

    const calc = (x: number, y: number) => [x - window.innerWidth / 2, y - window.innerHeight / 2]

    return (
        <div onMouseMove={({clientX: x, clientY: y}) => set({xy: calc(x, y)})}>
            <animated.div className="card1" 
                // @ts-ignore
                style={{transform: props.xy.interpolate((xy) => `translate3d(${xy[0] / 10}px,${xy[1] / 10}px,0)`)}}
            />
        </div>
    );
}
RobC
  • 22,977
  • 20
  • 73
  • 80