Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using useSharedValue instead of useState #134

Open
pvedula7 opened this issue Dec 1, 2023 · 0 comments
Open

Using useSharedValue instead of useState #134

pvedula7 opened this issue Dec 1, 2023 · 0 comments

Comments

@pvedula7
Copy link

pvedula7 commented Dec 1, 2023

My goal is to utilize useSharedValue to mimic the functionality of use/setState within Vision Camera v3 frame processors.

With reanimated I would have this code which uses runOnJS to set the state variable which causes my component to re-render on each Camera frame.

const [poses, setPoses] = useState<SKRNMLKitVisionCameraPluginResultPoseItem[]>([]);
const setPosetoVar = async (framePose) => {
    setPoses(framePose);
  };

const frameProcessor = useFrameProcessor((frame) => {
  "worklet";
  const pose = scanSKRNMLKitPose(frame);
  runOnJS(setPosetoVar)(pose);
  
}, []);

I have tried using shared values like this, but it does not act in the same way as setState by re-rendering component when poses is updated.

const poses = useSharedValue<SKRNMLKitVisionCameraPluginResultPoseItem[]>([]);
const frameProcessor = useFrameProcessor((frame) => {
  "worklet";
  const pose = scanSKRNMLKitPose(frame);
  poses.value = pose
}, []);

I have also tried the setState within the createRunInJSFn which causes to camera to keep reinitializing each time.

const poses = useState<SKRNMLKitVisionCameraPluginResultPoseItem[]>([]);
const setPosetoVar = async (framePose) => {
    setPoses(framePose);
  };
  const setJSPose = Worklets.createRunInJsFn(setPosetoVar)

const frameProcessor = useFrameProcessor((frame) => {
  "worklet";
  const pose = scanSKRNMLKitPose(frame);
  setJSPose(pose)
}, []);

Is there a way to use useSharedValue to mimic the functionality of useState/setState? I am drawing on the camera image and need it to re-render each frame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant