number-flip icon indicating copy to clipboard operation
number-flip copied to clipboard

I got an error when I use `flipTo` function in a 'react' project

Open Jickey opened this issue 3 years ago • 8 comments

my react component code:

const FlipNumber = ({ number: realNumber, className }) => {
  const ref = useRef();
  const flipRef = useRef(null);
  useEffect(() => {
    if (!flipRef.current) {
      flipRef.current = new Flip({
       node: ref.current,
        from: 0,
        to: realNumber,
        duration: 1.5,
        separator: ',',
      });
    } else {
      // error when flipTo runs 
      flipRef.current.flipTo({ to: realNumber });
    }
  }, [realNumber]);
  return (
   <div
      ref={ref}
    />
  );
};

image

image

Jickey avatar Dec 22 '21 08:12 Jickey

I think this will work: https://codesandbox.io/s/snowy-tdd-gf9hp?file=/src/App.js:89-488

const FlipNumber = ({ number: realNumber }) => {
  const ref = useRef();
  const flipRef = useRef(null);
  useEffect(() => {
    flipRef.current = new Flip({
      node: ref.current,
      from: 0,
      to: realNumber,
      duration: 1.5,
      separator: ","
    });
  }, []);
  useEffect(() => {
    flipRef.current.flipTo({ to: realNumber });
  }, [realNumber]);

  return <div ref={ref} />;
};

gaoryrt avatar Dec 23 '21 02:12 gaoryrt

Yeah. It does work in my 'vite' demo project but I failed in my production project, which meas it may be an incompatibility problem. Forget it. Thanks very much for your reply !

Jickey avatar Dec 24 '21 07:12 Jickey

any updated?

xmsz avatar Dec 06 '22 11:12 xmsz

@xmsz I believe this works effectively

gaoryrt avatar Dec 07 '22 03:12 gaoryrt

@xmsz I believe this works effectively

image

xmsz avatar Dec 07 '22 03:12 xmsz