In React-Native, I am creating a functional component called ImageSelector, in which I am using expo-image-picker and using the image URI as a required field in a parent component using Formik. My simulator works and I am able to successfully pick a generic image and log the image URI in the console ('success: ' + result.uri) but here are the following errors:
I want to display the image in the image component below but the image does not display (it does not break). I get the following error Unhandled promise rejection: ReferenceError: Can't find variable: props which I suppose is referring to the parent form component but I do not know what to change to get this error to go away.
Parent Component
import { View, Text, Button } from 'react-native';
import { Formik } from 'formik';
import * as yup from 'yup';
import ImageSelector from '../shared/imagePicker';
const newPostSchema = yup.object({
image: yup.string()
.required(),
})
export default function CreatePost({navigation}) {
const setImageURI = (image) => {
props.setFieldValue('imageUri', image.uri)
}
return (
<View style={styles?.container}>
<Formik
initialValues={{
imageURI: null,
}}
validationSchema={newPostSchema}
onSubmit={(values, actions) => {
console.log(values);
navigation.navigate('ReviewPost', {
imageURI: values.imageURI,
});
}}
>
{props => (
<View>
<ImageSelector
image={props.values.imageURI}
onImagePicked={setImageURI}
/>
<Button onPress={props.handleSubmit} title='REVIEW' />
</View>
)}
</Formik>
</View>
)
}
*** Nested ImageSelector Component in another file ***
import React, {useState, useEffect} from 'react';
import {View, Button, Image, StyleSheet} from 'react-native';
import * as ImagePicker from 'expo-image-picker';
const ImageSelector = ({image, onImagePicked}) => {
const [selectedImage, setSelectedImage] = useState();
useEffect(() => {
if(image) {
console.log('useEffect: ' + image);
setSelectedImage({uri: image});
}
}, [image])
pickImageHandler = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
title: 'Choose Image',
maxWidth: 800,
maxHeight: 600,
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1
});
if (!result.cancelled) {
console.log('success: ' + result.uri);
onImagePicked({uri: result.uri});
console.log('a');
setSelectedImage({uri: result.uri});
console.log('b');
}
if (result.cancelled) {
console.log('result cancelled: ' + result.cancelled);
}
}
return (
<View style={styles.container}>
<View style={styles.imageContainer}>
<Image source={selectedImage} />
</View>
<View style={styles.button}>
<Button title='Pick Image' onPress={this.pickImageHandler} />
</View>
</View>
)
}
The following 4 lines do not execute (console logs are for testing to ensure they don't get called):
- onImagePicked({uri: result.uri});*
- console.log('a'); *
- setSelectedImage({uri: result.uri});*
- console.log('b');*
I need to get the props-related error to go away, set selectedImage to equal result.uri, and have the image display in the <Image />
component using selectedImage.uri as the image source.
Help?