Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
Last active January 5, 2020 03:20
Show Gist options
  • Save jsoneaday/943968d2ae8da1cfa8aa39aa261f90a9 to your computer and use it in GitHub Desktop.
Save jsoneaday/943968d2ae8da1cfa8aa39aa261f90a9 to your computer and use it in GitHub Desktop.
import React, { useCallback, useEffect } from 'react';
import { CameraResultType } from '@capacitor/core';
import { useCamera, availableFeatures } from '@ionic/react-hooks/camera';
import { IonButton, IonContent, IonHeader, IonToolbar, IonTitle, IonPage, IonText } from '@ionic/react';
const Camera: React.FC = () => {
const { photo, getPhoto } = useCamera();
const triggerCamera = useCallback(async () => {
if(availableFeatures.getPhoto) {
await getPhoto({
quality: 100,
allowEditing: false,
resultType: CameraResultType.Uri
});
}
}, [getPhoto]);
useEffect(() => {
console.log('photo', photo);
}, [photo]);
const onClick = () => {
triggerCamera();
}
if(availableFeatures.getPhoto) {
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Camera</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
{photo && <img src={photo.webPath} alt="my pic" />}
<IonButton onClick={onClick}>
camera
</IonButton>
</IonContent>
</IonPage>
);
}
return (
<IonPage>
<IonContent>
<IonText>No Camera Available</IonText>
</IonContent>
</IonPage>
);
}
export default Camera;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment