Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
React - Update custom hook's ref in a component

I have implemented the following custom hook:

export default function useCamera() {
   const [cameraRatio, setCameraRatio] = useState(Camera.Constants.DesiredRatio);
   const [cameraType, setCameraType] = useState(Camera.Constants.Type.back);
   const [flashMode, setFlashMode] = useState(
      Camera.Constants.FlashMode.off
   );

   const camRef = useRef(null); <----- Interesting part
   const cameraTypeRef = useRef(cameraType);
   const isTakingPhoto = useRef(false);

   const takePhoto = () => {
      if(!camRef.current) return; <----- Interesting part

      ...
   }

   const flip = () => {}

   const toggleFlash = () => {}

   ...

   return {
      camRef,
      cameraRatio,
      cameraType,
      flashMode,
      takePhoto,
      flip,
      toggleFlashMode,
      ...
   }
}

So, in my Camera component, I just do:

function Camera({ ... }) {
   const {
      camRef, <---- Interesting part
      takePhoto,
      ...
   } = useCamera();
  
   return <NativeCamera ref={camRef} onTakePhoto={takePhoto} ... />
}

As you can see, I am passing the camRef to "NativeCamera". This ref is defined in my custom hook "useCamera", and received in my "Camera" component, which just pass the ref to the child.

But this doesn't work. Any workaround?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!