Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
react-native uploady demo - upload
const Upload = () => {
const [uploadUrl, setUploadUrl] = useState(false);
const uploadyContext = useContext(UploadyContext);
useItemFinishListener((item) => {
const response = JSON.parse(item.uploadResponse.data);
console.log(`item ${item.id} finished uploading, response was: `, response);
setUploadUrl(response.url);
});
useItemErrorListener((item) => {
console.log(`item ${item.id} upload error !!!! `, item);
});
useItemStartListener((item) => {
console.log(`item ${item.id} starting to upload,name = ${item.file.name}`);
});
const pickFile = useCallback(async () => {
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.images],
});
uploadyContext.upload(res);
} catch (err) {
if (DocumentPicker.isCancel(err)) {
console.log("User cancelled the picker, exit any dialogs or menus and move on");
} else {
throw err;
}
}
}, [uploadyContext]);
return (
<View>
<Button title="Choose File" onPress={pickFile} />
{uploadUrl && <Image source={{ uri: uploadUrl }} style={styles.uploadedImage} />}
</View>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment