Skip to content

[Question] Can I use variable which is derived from useRafLoop on useEffect? #2682

@taehoonkim1007

Description

@taehoonkim1007

Hi, I have a question about this situation below.

import { useState, useEffect } from "react";
import { useRafLoop } from "react-use";

export default function AppleDrop() {
  const [distance, setDistance] = useState(4000);
  const [loopStop] = useRafLoop(() => {
    setDistance(distance => distance -20);
  });

  useEffect(() => {
    if (distance <= 200) {
      loopStop();  <--- Is there no problem with using like this?
    }
  }, [distance]);
  return (
    /.../
  );
}

It works fine now, but I’m not sure if this is the way react-use recommends using it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions